formtastic不能将符号转换为整数

时间:2011-05-22 23:05:14

标签: ruby-on-rails nested-forms formtastic

我使用formtastic有一个相当简单的嵌套表单。

我的模特

class FieldMap < ActiveRecord::Base

  has_many :merge_splits
  accepts_nested_attributes_for :merge_splits

class MergeSplit < ActiveRecord::Base

    belongs_to :field_map

end

视图是

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

当我提交表单时,参数为

{"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"}

返回一个无法将符号转换为整数错误。 不幸的是,它没有告诉我它想要转换什么。

1 个答案:

答案 0 :(得分:1)

替换:

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

使用:

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

仅供参考,由于你的参数的结构,我确信错误在这里撒谎:

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

has_many关系中,嵌套属性如下:

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

修改

在你的控制器中,我想你会这样做:

@field_map = FieldMap.new

相同的逻辑,您应该有以下内容来创建merge_split:

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

然后移除:name