嵌套表单触发“无法批量分配受保护属性”警告
我有一个多层嵌套表单
User->Tasks->Prerequisites
,并且在相同的表单中
User->Tasks->Location
位置表单工作正常,现在我正在尝试指定当前任务的先决条件。先决条件是存储在 :completed_task 字段中的 task_id。
当我提交表单时,我在输出中收到以下错误:
WARNING: Can't mass-assign protected attributes: prerequisite_attributes
针对用户中的每项任务发出一个警告。
我已经解决了与此相关的所有其他问题,确保正确引用字段名称:completed_task,
将 attr_accessible 添加到我的模型中(它已经存在并且我扩展了它)。
我不知道我还应该做什么。
我的模型看起来像是
class Task < ActiveRecord::Base attr_accessible :user_id, :date, :description, :location_id belongs_to :user has_one :location accepts_nested_attributes_for :location has_many :prerequisites accepts_nested_attributes_for :prerequisites end class Prerequisite < ActiveRecord::Base attr_accessible :completed_task belongs_to :task end
使用 formtastic 的表单,并且我通过
<%= f.semantic_fields_for :prerequisites do |builder3| %> <%= render 'prerequisite_fields', :f=>builder3 %> <% end %> --- _prerequisite_fields.html.erb ----- < div class="nested-fields" > <%= f. inputs:completed_step %> </div>
任何建议包含该表单?
I've got a multi layer nested form
User->Tasks->Prerequisites
and in the same form
User->Tasks->Location
The location form works fine, now I'm trying to specify prerequisites to the current task. The prerequisite is a task_id stored in the :completed_task field.
When I submit the form, I get the following error in the output
WARNING: Can't mass-assign protected attributes: prerequisite_attributes
One warning for each task in the user.
I've gone through all the other questions related to this, ensuring that the field name :completed_task is being referenced correctly,
adding attr_accessible to my model (it was already there and I extended it).
I'm not sure what else i'm supposed to be doing.
My models look like
class Task < ActiveRecord::Base attr_accessible :user_id, :date, :description, :location_id belongs_to :user has_one :location accepts_nested_attributes_for :location has_many :prerequisites accepts_nested_attributes_for :prerequisites end class Prerequisite < ActiveRecord::Base attr_accessible :completed_task belongs_to :task end
the form uses formtastic, and I'm including the form via
<%= f.semantic_fields_for :prerequisites do |builder3| %> <%= render 'prerequisite_fields', :f=>builder3 %> <% end %> --- _prerequisite_fields.html.erb ----- < div class="nested-fields" > <%= f. inputs:completed_step %> </div>
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 :precession_attributes 添加到 attr_accessible 以便批量分配
attr_accessible :user_id, :date, :description, :location_id, :precession_attributes
应该可以帮助您入门。
Add :prerequisite_attributes to attr_accessible in order to mass-assign
attr_accessible :user_id, :date, :description, :location_id, :prerequisite_attributes
Should get you started.