Reputation: 2118
I am building a form to edit several records at once.
The records of the Mapping class are children of the records of the MappingsList class:
class Mapping < ActiveRecord::Base
...
belongs_to :mappings_list
.
class MappingsList < ActiveRecord::Base
...
has_many :mappings
In order to select the set of records to edit, from the mappings_list show view, I pass the mappings_list_id to the mappings controller:
class MappingsController < ApplicationController
...
# GET /mappings/1/edit
def edit
@mappings_list = MappingsList.find(params[:id])
@mappings_batch = Array(@mappings_list.mappings.each)
end
But when calling the form:
<%= form_for [@mappings_list, @mappings_batch] do |f| %>
<--! Loop with details mappings data to update -->
<% @mappings_batch.each do |map| %>
<% fields_for map do m %>
<div class="row">
<div class="span2 field"> <%= m.text_field :source_code, :disabled => true %>
</div>
<div class="span2 field"> <%= m.text_field :source_caption, :disabled => true %>
</div>
<div class="span2 field"> <%= m.collection_select :target_caption, @target_values, :value_caption, :value_caption %>
</div>
</div>
<hr/>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I get the error message:
Showing ...MyApp/app/views/mappings/_form.html.erb where line #1 raised:
undefined method `model_name' for Array:Class
Which I don't understand.
Can someone give me some hints about how to solve this ?
Thanks a lot,
Best regards,
Frédéric
Upvotes: 0
Views: 337
Reputation: 7054
Why [@mappings_list, @mappings_batch] ? It's wrong! Also you have a lot of other mistakes.
The right solution is:
<%= form_for @mappings_list do |f| %>
<%= f.fields_for :mappings do |m| %>
<div class="row">
<div class="span2 field"> <%= m.text_field :source_code, :disabled => true %>
</div>
<div class="span2 field"> <%= m.text_field :source_caption, :disabled => true %>
</div>
<div class="span2 field"> <%= m.collection_select :target_caption, @target_values, :value_caption, :value_caption %>
</div>
</div>
<hr/>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And don't forget:
class MappingsList < ActiveRecord::Base
...
accepts_nested_attributes_for :mappings
Upvotes: 1