另一个Rails 3嵌套表单问题



所以我有以下内容:

class ComplexAssertion < ActiveRecord::Base
  has_many :assertion_groups
  has_many :assertions, :through => :assertion_group
class AssertionGroup < ActiveRecord::Base
  belongs_to :complex_assertion
  has_many :assertions, :dependent => :destroy, :autosave=>true
class Assertion < ActiveRecord::Base
  belongs_to :assertion_group

我的表格如下:

<%= form_for(@complex_assertion) do |f| %>
<div class="childGroup1" style="padding:5px;">
      <%= f.fields_for :assertion_groups do |assertion_group_fields| %>
      <%= assertion_group_fields.fields_for :assertions do |assertion_fields| %>
      Type: <%= assertion_fields.collection_select :assertion_type_id, AssertionType.all, :id, :name %>
      Attribute: <%= assertion_fields.collection_select :attribute_name, Attribute.find_by_sql("select distinct a.name from attributes a "), :name, :name %>
      <%= assertion_fields.label :operator_type_id %>: <%= assertion_fields.collection_select :operator_type_id, OperatorType.all, :id, :value %>
      Value: <%= assertion_fields.text_field :value, :size=>'1' %>
      <% end %>
          <div id="innerOperator">
            <!--OPERATOR BETWEEN EACH ASSERTION IN A GROUP-->
            <%= assertion_group_fields.collection_select :logical_operator_type_id, LogicalOperatorType.all, :id, :value %>
          </div>
      <% end %>
    </div>
    <div id="createComplex" align="center">
       <%= f.submit :value=>'Submit' %>
    </div>
<% end %>

当我尝试保存时,我得到以下错误:

AssertionGroup(#48180276) expected, got Array(#19330344)
Rails.root: C:/Eagle6/Assertions
Application Trace | Framework Trace | Full Trace
app/controllers/complex_assertions_controller.rb:43:in `new'
app/controllers/complex_assertions_controller.rb:43:in `create'

我已经尝试进行多次更改以使其正确工作,但我认为我在这里缺少了一些非常基本的逻辑。有人能给我指正确的方向吗?我的印象是,如果我正确地完成了模型并使用了Form_builders,Rails就会处理插入和关系。

谢谢!

添加了控制器代码-非常普通。

def create
@complex_assertion = ComplexAssertion.new(params[:complex_assertion])
respond_to do |format|
  if @complex_assertion.save
    format.html { redirect_to(@complex_assertion, :notice => 'Complex assertion was successfully created.') }
    format.xml  { render :xml => @complex_assertion, :status => :created, :location => @complex_assertion }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @complex_assertion.errors, :status => :unprocessable_entity }
  end
end
end

您的ComplexAssertion类应该看起来像这个

class ComplexAssertion < ActiveRecord::Base
  has_many :assertion_groups
  has_many :assertions, :through => :assertion_group
  accepts_nested_attributes_for :assertion_groups # <= add this

你的AssertionGroup类应该像这个

class AssertionGroup < ActiveRecord::Base
  belongs_to :complex_assertion
  has_many :assertions, :dependent => :destroy, :autosave=>true
  accepts_nested_attributes_for :assertions # <= add this

您可以在http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html("嵌套属性示例"、"一对多")。

相关内容

  • 没有找到相关文章

最新更新