2012-07-09 113 views
1

有計算器上軌左右複雜的形式有很多問題,他們都似乎指向對這個問題Ryan Bates的不錯railscast演示:part1part2如何在rails中構建複雜的表單,以便我可以關聯或創建新的相關實體?

這是偉大的它做什麼,但我沒有看到解決,你可能需要創建新的兒童的狀況問題的對象,或者您可能希望已經存在的對象相關聯。

在我來說,我希望用戶能夠創建一個新的事件。作爲其中的一部分,他們需要說出誰參與了事件。大約一半的時間,被添加到事件中的人員已經存在於數據庫中。在這種情況下,應鼓勵用戶使用這些現有記錄與創建新記錄。

如何處理這種形式的複雜性有什麼建議?

而作爲解決方案的一部分,你建議,如果用戶沒有找到該應用程序繼續運行並前的父對象被提交創建它在飛行的人嗎?我的想法(但有興趣聽取建議)是,用戶應該使用現有的人員記錄(如果有),但如果用戶需要創建新人員記錄,則該新記錄不會提交給數據庫,直到用戶提交事件。思考?

回答

2

喜來完成你需要做什麼,有幾個簡單的步驟如下: 1)在模型類(事件和人物類,你的情況),請確保你把這樣的事情:

class Incident < ActiveRecord::Base 
    has_and_belongs_to_many :people 
    # Note if you want to be able to remove a person from the Incident form (to de-associate) put true in :allow_destroy => true on the accepts_nested_attributes_for 
    accepts_nested_attributes_for :people, :allow_destroy => false, :reject_if => :all_blank 
    validates_associated :people #so you won't be able to save invalid people objects 
    attr_accessible :date_occur, :location, :people_attributes # note the :people_attributes here 
    #do your Incident validations as usual...   
end 

class Person < ActiveRecord::Base 
    has_and_belongs_to_many :incidents 
    #the following line it's to allow mass assignment, basically it will allow you to create people from the Incident form 
    attr_accessible :first_name, :last_name, :dob 
    #do your Person validations as usual... 
end 

2)在視圖方面,最簡單的方法是修改事件表單文件(app/view/incidents/_form.html.erb),以允許用戶將現有的和新的人員分配給事件:

# app/view/incidents/_form.html.erb 

<%= semantic_form_for(@incident) do |f| %> 
    <% if @incident.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@incident.errors.count, "error") %> prohibited this incident from being saved:</h2> 
     <ul> 
      <% @incident.errors.full_messages.each do |msg| %> 
       <li><%= msg %></li> 
      <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
     <%= f.label :date_occur %><br /> 
     <%= f.datetime_select :date_occur %> 
    </div> 
    <div class="field"> 
     <%= f.label :location %><br /> 
     <%= f.text_field :location %> 
    </div> 

    <%= f.input :people, :as => :select, :collection=>Hash[Person.all.map { |p| [p.first_name + ' - ' + p.last_name, p.id] }] %> 

    <%= f.fields_for :people, Person.new() do |new_person_form| %> 
     <div class="incident-people new-person"> 
      <%= new_person_form.inputs :name=>'Add New person' do %> 
       <%= new_person_form.input :first_name, :label=>'First Name: ' %> 
       <%= new_person_form.input :last_name, :label=>'Last Name: ' %> 
       <%= new_person_form.input :dob, :label=>'Date of birth: ' %> 
      <% end %> 
     </div> 
    <% end %> 


    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

3)最後,你需要修改你的更新和Cr eate方法的事件控制器如下:

# app/controllers/incident_controller.rb 
def create 
    selected_people = params[:incident][:person_ids].keep_if{ |v| v.present? } 
    params[:incident].delete(:person_ids) 
    @incident = Incident.new(params[:incident]) 
    @incident.people = Person.find(selected_people) 

    respond_to do |format| 
     if @incident.save 
      format.html { redirect_to @incident, notice: 'Incident was successfully created.' } 
      format.json { render json: @incident, status: :created, location: @incident } 
     else 
      format.html { render action: "new" } 
      format.json { render json: @incident.errors, status: :unprocessable_entity } 
     end 
    end 
end 

def update 
    selected_people = params[:incident][:person_ids].keep_if{ |v| v.present? } 
    params[:incident].delete(:person_ids) 
    @incident = Incident.find(params[:id]) 
    @incident.people = Person.find(selected_people) 
    respond_to do |format| 
     if @incident.update_attributes(params[:incident]) 
      format.html { redirect_to @incident, notice: 'Incident was successfully updated.' } 
      format.json { head :no_content } 
     else 
      format.html { render action: "edit" } 
      format.json { render json: @incident.errors, status: :unprocessable_entity } 
     end 
    end 
end 

而就是這樣,讓我知道如果你需要進一步的幫助。 FedeX

+0

嗨聯邦快遞,感謝您的幫助。不知道我是否理解正確。此代碼是否允許用戶爲事件選擇現有人員?如果是這樣,我沒有跟着。哪裏是? – snowguy 2012-07-11 16:27:03

+0

我的壞雪,我完全忘了你需要那個呵呵!請現在看看,我更新了所有你需要的答案! – FedeX 2012-07-12 01:28:34

+0

我需要研究一下,但看起來就像我在找什麼。謝謝你的幫助! – snowguy 2012-07-12 05:06:20

相關問題