简体   繁体   English

使用accepts_nested_attributes_for更新关联的问题

[英]Issues using accepts_nested_attributes_for to update associations

I'm trying to build a form that updates an association when updating the parent object. 我正在尝试构建一种在更新父对象时更新关联的表单。 I've been trying to use the accepts_nested_attributes_for option as well as attr_accessible but am still running into a Can't mass-assign protected attributes error. 我一直在尝试使用accepts_nested_attributes_for选项以及attr_accessible但是仍然遇到Can't mass-assign protected attributes错误。

Here are my models: 这是我的模型:

class Mastery < ActiveRecord::Base
  attr_accessible :mastery_id,
                  :name,
                  :icon,
                  :max_points,
                  :dependency,
                  :tier,
                  :position,
                  :tree,
                  :description,
                  :effects_attributes
  has_many :effects, :as => :affects, :dependent => :destroy, :order => 'effects.value'
  accepts_nested_attributes_for :effects
end


class Effect < ActiveRecord::Base
  attr_accessible :name,
                  :modifier,
                  :value,
                  :affects_id,
                  :affects_type
  belongs_to :affects, :polymorphic => true
end

Here's the partial that's rendering the form: 这是呈现表单的部分内容:

<%= semantic_form_for [ :manage, resource ], :html => {:class => 'default-manage-form' } do |f| %>
  <%= f.inputs do %>
    <% attributes.each do |attr| %>
      <%= f.input attr.to_sym %>
    <% end %>

    <% if resource.respond_to? :effects %>
      <% resource.effects.each do |effect| %>
        <hr>
        <%= f.inputs :modifier, :name, :value, :for => effect %>
      <% end %>
    <% end %>

    <%= f.actions do %>
      <%= f.action :submit %>
    <% end %>
  <% end %>
<% end %>

My form is for the Mastery record which contains multiple Effect records. 我的表单用于掌握记录,其中包含多个效果记录。 Can anyone see why I'd be running into this error and what I can do to fix it? 谁能看到我为什么会遇到此错误以及如何解决该错误?

I've resolved this by doing two things: 我通过做两件事解决了这个问题:

1) Changing the form structure to use fields_for and 1)更改表单结构以使用fields_for

2) Adding :effects_attributes to the attr_accessible for the Mastery model 2)将:effects_attributes添加到精通模型的attr_accessible

Here's the new form code: 这是新的表单代码:

<%= semantic_form_for [ :manage, resource ], :html => {:class => 'default-manage-form' } do |f| %>
  <%= f.inputs do %>
    <% attributes.each do |attr| %>
      <%= f.input attr.to_sym %>
    <% end %>

    <% if resource.respond_to? :effects %>
      <%= f.fields_for :effects do |b| %>
        <hr>
        <%= b.inputs :modifier, :name, :value %>
      <% end %>
    <% end %>

    <%= f.actions do %>
      <%= f.action :submit %>
    <% end %>
  <% end %>
<% end %> 

And finished model: 并完成模型:

class Mastery < ActiveRecord::Base
  attr_accessible :name,
                  :icon,
                  :max_points,
                  :dependency,
                  :tier,
                  :position,
                  :tree,
                  :description,
                  :effects_attributes
  has_many :effects, :as => :affects, :dependent => :destroy, :order => 'effects.value'
  accepts_nested_attributes_for :effects
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM