2011-02-09 56 views
1

我正在rails 3中构建一个多嵌套的表单。我使用formtastic_cocoon gem,但我不认为这对这个问题有很大的影响。深嵌套表格数据没有进入数据库,没有错误

我有用户,用户有任务,任务有步骤。 嵌套是用户>任务>步骤。 我可以动态添加和删除任务字段给用户,以及任务中的步骤字段。

但是,当我提交表单时,用户获取任务,但任务>步骤不会保存到数据库。

Rails没有返回任何错误,只是没有任何反应。

我的模型

 
Class User < ActiveRecord::Base 
    acts_as_authentic 

    has_many :tasks 
     accepts_nested_attributes_for :tasks, :reject_if=> proc {|attributes| attributes[:entry].blank?}, :allow_destroy => true 

end 

Class Task < ActiveRecord::Base 
      attr_accessible :entry 

      belongs_to :user 
      has_many :steps 
     accepts_nested_attributes_for :steps, :reject_if=> proc {|attributes| attributes[:title].blank?}, :allow_destroy => true 
end 

Class Step < ActiveRecord::Base 
     attr_accesible :title 

     belongs_to :task 
end 

在我form.html.erb我有

 
<%= semantic_form_for @user %> 
    <%= form.inputs :username, :password %> 
    <div> 
     <% form.semantic_form_fields_for :tasks do |builder| %> 
     <%= render 'task_fields', :f=>builder %> 
     <% end %> 
    <%= link_to_add_association 'add task', form, :tasks %> 
    </div> 

的_task_fields.html.erb看起来像这样

 
<div class="nested-fields"> 
    <%= link_to_remove_association "remove task", f %> 
     <%= f.inputs :entry %> 
      <div> 
      <% f.semantic_form_fields_form :steps do |builder| %> 
       <%= render 'step_fields' :f => builder %> 
      <% end %> 
      <%= link_to_add_association 'add step', f, :steps %> 
      </div> 
</div> 

最后,_step_fields .html.erb页面是

 
    <div class="nested-fields"> 
    <%= link_to_remove_association "remove step", f %> 
    <%= f.inputs :title %> 
    </div> 

回答

1

你的日志?:

WARNING: Can't mass-assign protected attributes: steps_attributes 

如果是在看到这种情况,这增加了任务模式:

attr_accessible :steps_attributes 
+0

我没有看到任何“警告:”或任何错误信息。我在哪里可以找到? 你说的没错,解决了这个问题。谢谢! – pedalpete 2011-02-10 02:34:57

相关问题