我有一个具有嵌套形式的表单:
<%- for subscription in @task.subscriptions -%>
<%- semantic_fields_for "task[subscription_attributes][]", subscription do |subscription_form|%>
<%- subscription_form.inputs do -%>
<%= subscription_form.input :workhours, :label => subscription.user.full_name%>
<%- end -%>
<%- end -%>
<%- end -%>
在任务模型上我有:
accepts_nested_attributes_for :subscriptions
attr_accessible :mission_id, :statuscode_id, :name, :objectives, :start_at , :end_at, :hours, :testimony ,:subscriptions_attributes
在表单(视图)中,我在workhours字段中获得了正确的值。但是当我更改值并点击提交按钮时,值永远不会改变。 我无法弄清楚为什么......
我在控制台中看到这是在构建任务属性。所以值传递给控制器。
"subscription_attributes"=>{"11"=>{"workhours"=>"20"}, "12"=>{"workhours"=>"303"}, "9"=>{"workhours"=>"120"}, "10"=>{"workhours"=>"10"}}
在tasks_controller上我有:
@task.update_attributes(params[:task])
答案 0 :(得分:0)
根据NestedAttributes api页面上提供的示例,我认为您的subscription_attributes
更新应该更像这样
"subscription_attributes" => [ {"id" => "11", "workhours"=>"20"}, {"id" => "12", "workhours"=>"303"}, {"id" => "9", "workhours"=>"120"}, {"id" => "10", "workhours"=>"10"} ]
因此,您可能需要更改表单以传递更新的ID。