0
votes

I've got a fairly simple nested form using formtastic.

My models are

class FieldMap < ActiveRecord::Base

  has_many :merge_splits
  accepts_nested_attributes_for :merge_splits

class MergeSplit < ActiveRecord::Base

    belongs_to :field_map

end

view is

<%= semantic_form_for @field_map do |f| %>

    <%= f.input :base_field, :collection => @base_fields %>
    <%= f.input :master_field, :collection =>@master_fields %>

        <%= f.semantic_fields_for :merge_split do |ms| %>
        <%= ms.input :action, :collection => @actions, :required=> false, :input_html=>{:class=>'split_merge_type', :name=>"field_map[merge_splits_attributes][action]"} %>
        <%= ms.input :character, :required=> false, :input_html=>{:class=>'split_merge_char', :name=>"field_map[merge_splits_attributes][character]"} %>
        <%= ms.input :count, :collection => [0,1], :include_blank=>false, :required=> false, :input_html=>{:class=>'split_merge_count', :name=>"field_map[merge_splits_attributes][count]"} %>
    <% end %>


    <%= f.submit %>
<% end %>

When I submit my form, the parameters are

{"utf8"=>"?",
 "authenticity_token"=>"lNinAw4DpjkIHsuUEaa0xo+sRG+qk3elA4T2VfCWgXc=",
 "field_map"=>{"base_field_id"=>"18",
 "master_field_id"=>"11",
 "merge_splits_attributes"=>{"action"=>"0",
 "character"=>"ti",
 "count"=>"0"},
 "attribute_map_attributes"=>{"attribute_id"=>""}},
 "commit"=>"Create Field map"}

which returns a can't convert symbol to integer error. Unfortunately, It doesn't tell me what it is trying to convert.

1
I added the view, I assume you meant the view for the form, as the error is coming from 'create'.pedalpete

1 Answers

1
votes

Replace:

<%= f.semantic_fields_for :merge_split do |ms| %>

With:

<%= f.semantic_fields_for :merge_splits do |ms| %>

Just FYI, I was sure the error lied here because of the structure of your params:

"merge_splits_attributes"=>{"action"=>"0", "character"=>"ti", "count"=>"0"}

In a has_many relationship, the nested attributes are like this:

"merge_splits_attributes"=>{"0" => {"action"=>"0", "character"=>"ti", "count"=>"0" }, "1" => {"action"=>"0", "character"=>"ti", "count"=>"0" } }

EDIT

In your controller, I guess you do:

@field_map = FieldMap.new

Same logic, you should have the following to have a merge_split to create:

@field_map = FieldMap.new
@field_map.merge_splits.build

And remove the :name