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

人盡茶涼 提交于 2019-12-25 07:39:33

问题


I have two models (lead which has_many :quote_metals and accepts_nested_attributes_for :quote_metals and quote_metal which belongs_to :lead).

I am trying to write the information submitted in the form to the different datatables. There should just be one lead and multiple (no exact number) of quote_metals. I am receiving an error saying that Unpermitted parameter: quote_metal.

Here are the parameters:

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"}

Here is my controller:

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

Here is the form:

<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>

and the partial:

<%= 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 %>

The lead model is writing to its datatable. What am I doing wrong causing the quote_metal not to write?


回答1:


The error is in this line <%= f.fields_for 'quote_metal[]', @quote_metal do |ff| %>. It should be

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


来源:https://stackoverflow.com/questions/31388010/unpermitted-parameter-in-rails-4-when-submitted-nested-attributes-has-many-asso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!