has_manyのオブジェクトを更新する Rails
移転しました。
●モデル
Person has_many :sections Section belongs_to :person
●ビュー
<% @person.sections.each do |@section| %> <%= text_field "section[]", "section_title" %> <%= text_area "section[]", "section_content" %> <% end %>
●コントローラー(update)
def update @person = Person.find(@params[:id]) update_result = @person.update_attributes(@params[:person]) @person.sections.collect do |section| update_result &&= section.update_attributes(@params[:section][section.id.to_s]) end if update_result flash[:notice] = 'Person was successfully updated.' redirect_to :action => 'show', :id => @person else render :action => 'edit' end end
参考URL
http://techno.hippy.jp/rorwiki/?HowToUpdateModelsWithHasManyRelationships