Unpermitted parameter in Rails 4 提交嵌套属性时,has_many关联表单

Unpermitted parameter in Rails 4 when submitted nested attributes, has_many associated form

我有两个模型(lead has_many :quote_metalsaccepts_nested_attributes_for :quote_metals 以及 quote_metal belongs_to :lead)。

我正在尝试将表单中提交的信息写入不同的数据表。应该只有一个线索和 quote_metals 的多个(没有确切数字)。我收到一条错误消息,指出 Unpermitted parameter: quote_metal.

参数如下:

Processing by LeadsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"h5iBI7teODFFXRk91puydlr4UmC9EhU5aR9ul2D0u4STFp68GV0wOgb/I43Ukga2MHjLWg1ZnbPeLwuLmMxrkw==", "lead"=>{"name"=>"Biggie", "email"=>"test@test.com",...., "note"=>"asdf", "quote_metal"=>[{"metal"=>"iron", "weight"=>"1", "unit"=>"pounds"}, {"metal"=>"ore", "weight"=>"4", "unit"=>"KG"}]}, "commit"=>"Submit"}

这是我的控制器:

class LeadsController < ApplicationController

      def index
        @lead = Lead.new
        @quote_metal = @lead.quote_metals.build
      end

      def create
        #raise params.inspect
        @lead = Lead.create!(lead_params)
      end

      def show
      end


private

    def lead_params
      params.require(:lead).permit([:name, :email, :phone, :zip, :note, :method, quote_metals_attributes: [:id, :metal, :weight, :unit, :price]])
    end

end

表格如下:

<div class="container">
  <div class="row">

    <div class="col-md-6">

      <%= form_for @lead, class: 'form-horizontal' do |f| %>
        <div class="form-group">
          <%= f.label :name %>
          <%= f.text_field :name, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :email %>
          <%= f.email_field :email, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :phone %>
          <%= f.telephone_field :phone, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :zip %>
          <%= f.text_field :zip, class: "form-control" %>
        </div>

        <div class="form-group">
            <%= f.label :method %><br>
            <%= f.select :method, Lead.methods.map { |k,v| [k.humanize, k] }, {},
            class: "form-control", :required => true %>
        </div>

        <div class="form-group">
          <%= f.label :notes %>
          <%= f.text_area :note, class: "form-control" %>
        </div>

          <%= render :partial => "metalLead", locals: {f: f} %>
          <%= render :partial => "metalLead", locals: {f: f} %>

        <div class="form-group text-center">
          <%= f.submit "Submit" %>
        </div>
      <% end %>


    </div>
  </div>
</div>

和部分:

<%= f.fields_for 'quote_metal[]', @quote_metal do |ff| %>
            <div class="form-group">
                  <%= ff.label :metal %><br>
                  <%= ff.select :metal, QuoteMetal.metals.map { |k,v| [k.humanize, k] }, {},
                  class: "form-control", :required => true %>
            </div>
          <!-- break -->
              <div class="form-group">
                  <%= ff.label :weight %><br>
                  <%= ff.number_field :weight, :required => true, step: 0.1, min: 0.0,
                  placeholder: '5.00', class: "form-control" %>
              </div>
          <!-- break -->                
            <div class="form-group">
                <%= ff.label :unit %><br>
                <%= ff.select :unit, QuoteMetal.units.map { |k,v| [k.humanize, k] }, {},
                class: "form-control", :required => true %>
            </div>
<% end %>

主要模型正在写入其数据表。我做错了什么导致 quote_metal 不写?

错误在这一行<%= f.fields_for 'quote_metal[]', @quote_metal do |ff| %>。应该是

<%= f.fields_for :quote_metals, @quote_metal do |ff| %>