2010-05-06 100 views
0

我很努力想出正確的方法來設計一個表格,這將允許我輸入兩個不同模型的數據。形式是爲一個「事件」,它具有以下關係:如何在一個表單中爲多個模型使用嵌套表單?

belongs_to    :customer 
belongs_to    :user 

has_one     :incident_status 

has_many    :incident_notes 
accepts_nested_attributes_for :incident_notes, :allow_destroy => false 

因此事件被分配到「客戶」和「用戶」,用戶能夠添加的「注意」到事件。我在表單的註釋部分遇到問題。這裏是如何的形式提交:

{"commit"=>"Create", 
"authenticity_token"=>"ECH5Ziv7JAuzs53kt5m/njT9w39UJhfJEs2x0Ms2NA0=", 
"customer_id"=>"4", 
"incident"=>{"title"=>"Something bad", 
"incident_status_id"=>"2", 
"user_id"=>"2", 
"other_id"=>"AAA01-042310-001", 
"incident_note"=>{"note"=>"This is a note"}}} 

這似乎是試圖將incident_note添加爲場下的「事件」,而不是一個incident_id外鍵鏈接回創建的incident_note表的新條目事件。

這裏是 'IncidentNote' 模式:

belongs_to :incident 
belongs_to :user 

這裏是 '事故' 的形式:

<% form_for([@customer,@incident]) do |f| %> 
    <%= f.error_messages %> 

    <p> 
    <%= f.label :other_id, "ID" %><br /> 
    <%= f.text_field :capc_id %> 
    </p> 
    <p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
     <%= label_tag 'user', 'Assign to user?' %> 
     <%= f.select :user_id, @users.collect {|u| [u.name, u.id]} %> 
    </p> 
    <p> 
     <%= f.label :incident_status, 'Status?' %> 
     <%= f.select :incident_status_id, @statuses.collect {|s| [s.name, s.id]} %> 
    </p> 
    <p> 
     <% f.fields_for :incident_note do |inote_form| %> 
     <%= inote_form.label :note, 'Add a Note' %> 
     <%= inote_form.text_area :note, :cols => 40, :rows => 20 %> 
     <% end %> 
    </p> 
    <p> 
    <%= f.submit "Create" %> 
    </p> 
<% end %> 

最後,這裏有新的incident_controller條目和創建。

新:

def new 
    @customer = current_user.customer 
    @incident = Incident.new 
    @users = @customer.users 
    @statuses = IncidentStatus.find(:all) 
    @incident_note = IncidentNote.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @incident } 
    end 
    end 

創建:

def create 
    @users = @customer.users 
    @statuses = IncidentStatus.find(:all) 
    @incident = Incident.new(params[:incident]) 
    @incident.customer = @customer 
    @incident_note = @incident.incident_note.build(params[:incident_note]) 
    @incident_note.user = current_user 

    respond_to do |format| 
     if @incident.save 
     flash[:notice] = 'Incident was successfully created.' 
     format.html { redirect_to(@incident) } 
     format.xml { render :xml => @incident, :status => :created, :location => @incident } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @incident.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

我真的不知道在哪裏看在這一點上。我相信這只是我當前Rails技能的限制(我不太瞭解)。所以如果有人能指出我的方向是正確的,我會非常感激。請讓我知道是否需要更多信息!

謝謝!

回答

1

檢查API的fields_for方法並滾動到一對多的部分。

您的模型有很多:incident_notes,而不是一個incident_note,這就是爲什麼它不理解關係,並試圖找到這個名稱的字段。

所以它應該是:

<% f.fields_for :incident_notes do |inote_form| %> 
    <%= inote_form.label :note, 'Add a Note' %> 
    <%= inote_form.text_area :note, :cols => 40, :rows => 20 %> 
    <% end %> 

它循環通過分配給所有事件和incident_notes爲他們每個人的生產領域。
您還必須建立至少一個音符在你new行動,否則會出現無:

def new 
    @incident = Incident.new 
    @incident.incident_notes.build 
    # ... 
    end 
+0

感謝您的答覆。我認爲這是問題。現在我收到了其他錯誤,因爲我修補了一些,所以我還沒有確認。 :) – Magicked 2010-05-06 16:59:45

相關問題