accepts_nested_attributes with Model.update for multiple models

Posted by Ohad on Stack Overflow See other posts from Stack Overflow or by Ohad
Published on 2010-03-15T04:34:36Z Indexed on 2010/03/15 4:39 UTC
Read the original article Hit count: 306

Hi,

I'm trying to follow http://railscasts.com/episodes/198-edit-multiple-individually but I would like to save objects which are nested (accepts_nested_attributes_for).

I've added the following in my controller:

def edit_multiple
  @people = Person.find(params[:person_ids], :include => [:parameters])
end

def update_multiple
  keys = params[:people].keys
  if keys.empty?
    flash[:error] = "Please select at least one person"
    redirect_to :back and return
  end
  values = keys.map {|k| params[:people][k]}

  @people = Person.update(keys,values).reject { |h| h.errors.empty? }

  if @people.empty?
    flash[:notice] = 'Updated people!'
    redirect_to person_path
  else
    redirect_to edit_multiple_path
  end
end

and in the view:

<% form_tag update_multiple_people_path, :method => :post do %>
  <% for person in @people %>
    <% fields_for "people[]", host do |f| %>
      <%= f.error_messages :object_name => "person" %>
      <h3><%= h person.name %></h3>
      <% for parameter in person.parameters %>
        <% f.fields_for "person_parameters[]", parameter  do |builder| -%>
           <%= render "common/parameters", :f => builder %>
        <% end -%>
      <% end -%>
      <p><%= link_to_add_fields "Add a parameter", f, :person_parameters, "common/parameters" %></p>
    <% end %>
  <% end %>
  <p><%= submit_tag "Edit these Parameter(s)" %></p>
<% end %>

but I'm always getting a mistmatch - e.g. ActiveRecord::AssociationTypeMismatch and Parameter(#70341811965140) expected, got Array(#70341874300460)

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about nested-attributes