开发者

saving nested attributes

开发者 https://www.devze.com 2022-12-25 05:53 出处:网络
I have a form that has a nested form like this: <%- for subscription in @task.subscriptions -%> <%- semantic_fields_for \"task[subscription_attributes][]\", subscriptiondo |subscription_form

I have a form that has a nested form like this:

<%- 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 -%>

And on the task model I have:

  accepts_nested_attributes_for :subscriptions
  attr_accessible :mission_id, :statuscode_id, :name, :objectives, :start_at , :end_at, :hours, :testimony ,:subscriptions_attributes

In the form (view) I get the correct values on the workhours fields. But when I change the values and hit the submit button, the values are never changed. I can't figure out why...

I see in the console that this is building up on the tasks attributes. So the values are passing to the controller.

"subscription_attributes"=>{"11"=>{"workhours"=>"20"}, "12"=>{"workhours"=>"303"}, "9"=>{"workhours"=>"120"}, "10"=>{"wo开发者_StackOverflowrkhours"=>"10"}}

On the tasks_controller I have:

@task.update_attributes(params[:task])


From the examples given on the NestedAttributes api page, I would think your subscription_attributes update should look more like this

"subscription_attributes" => [ {"id" => "11", "workhours"=>"20"}, {"id" => "12", "workhours"=>"303"}, {"id" => "9", "workhours"=>"120"}, {"id" => "10", "workhours"=>"10"} ]

So you might need to change up your form to pass the ids for the update.

0

精彩评论

暂无评论...
验证码 换一张
取 消