StaplesController中的ActionController :: UnpermittedParameters#在我使用多态关联的时候创建nested_attributes

时间:2015-04-03 12:55:14

标签: ruby-on-rails-4 nested-forms nested-attributes polymorphic-associations

我绝望了......我从所有可能的相关来源尝试了所有建议,但没有任何帮助。我使用ruby 2.0.0 rails 4.1.8和nested_form gem。

它仍会产生相同的错误:

ActionController :: StaplesController中的UnpermittedParameters #create

找到了未经许可的参数:cqnames

我有模型 Staple

class Staple < ActiveRecord::Base
  has_and_belongs_to_many :allergens
  has_many :cqnames, as: :cqnable
  accepts_nested_attributes_for :cqnames, allow_destroy: true
  translates :name, :description
  validates :name, presence: true, uniqueness: true
end

关联 has_and_belongs_to_many 以模拟过敏原

class Allergen < ActiveRecord::Base
  has_and_belongs_to_many :staples
  translates :name, :annex
  validates :code, presence: true, uniqueness: true, numericality: { only_integer: true, greater_than: 0 }
  validates :name, presence: true, uniqueness: true
end

(效果很好......)和多态关联来建模 Cqname

class Cqname < ActiveRecord::Base
  belongs_to :cqnable, polymorphic: true
end

允许在 staples_controller.rb 中使用nested_attributes:

  private
    def set_staple
      @staple = Staple.find(params[:id])
    end

    def staple_params
      params.require(:staple).permit(:name, :description, allergen_ids: [], cqnames_attributes: [:id, :name, :language, :description, :_destroy] )
    end

以下是错误屏幕的内容(在现有订书钉更新时生成):

ActionController :: StaplesController中的UnpermittedParameters#update

找到未经许可的参数:cqnames

提取的来源(第68行):

66 |
67 |     def staple_params
68 |         params.require(:staple).permit(:name, :description, allergen_ids: [], cqnames_attributes: [:id, :name, :language, :description, :_destroy] )
69 |     end
70 |  end

app / controllers / staples_controller.rb:68:in&#39; staple_params&#39;
app / controllers / staples_controller.rb:40:in&#39; update&#39;

请求

参数:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"MdufJeUWmoNdJaEeP/ReIxOY+TLthJ9yIUD9aI15Htg=",
 "staple"=>{"name"=>"Syr",
 "description"=>"Nejaká tá mliečna dobrôtka.",
 "allergen_ids"=>["1",
 "2",
 "3",
 ""],
 "cqnames_attributes"=>{"1428061910570"=>{"cqnames"=>{"name"=>"aaa",
 "language"=>"bbb",
 "description"=>"ccc",
 "_destroy"=>""}}}},  
 "commit"=>"Update Staple",  
 "locale"=>"en",  
 "id"=>"2"}

此处主题 _form.html.erb

<%= simple_nested_form_for(@staple) do |f| %>
  <% if @staple.errors.any? %>
    <div id="error_explanation">
      <h2><%= t('errors.template.header', count: @staple.errors.size, model: t('activerecord.models.staple')) %></h2>

      <ul>
        <% @staple.errors.full_messages.each do |message| %>
        <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.input :description %>
    <%= f.association :allergens, as: :check_boxes %>
    <%= f.simple_fields_for :cqnames %>
    <div data-no-turbolink><%= f.link_to_add "Add a colloquial name", :cqnames %></div>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

_cqname_fields.html.erb

<fieldset>
  <legend><%= _("Colloquial Name") %></legend>
  <%= f.simple_fields_for :cqnames do |builder| %>
    <%= builder.input :name %>
    <%= builder.input :language %>
    <%= builder.input :description, as: :text %>
    <%= builder.link_to_remove "Remove this colloquial name" %>
  <% end %>
</fieldset>

如果你知道,请帮助我......

1 个答案:

答案 0 :(得分:0)

对不起伙计......在 _form.html.erb _cqname_fields.html

中只是误用了 simple_fields_for 方法

正确的版本是......

<强> _form.html.erb

  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.input :description %>
    <%= f.association :allergens, as: :check_boxes %>
    <%= f.simple_fields_for :cqnames do |builder| %>
      <%= render 'cqname_fields', f: builder %>
    <% end %>
    <div data-no-turbolink><p><%= f.link_to_add "Add a colloquial name", :cqnames %></p></div>
  </div>

_cqname_fields.html

<fieldset>
  <legend><%= _("Colloquial Name") %></legend>
    <%= f.input :name %>
    <%= f.input :language %>
    <%= f.input :description, as: :text %>
    <%= f.link_to_remove "Remove this colloquial name" %>
</fieldset>

现在一切正常......; - )