在Rails 4中提交嵌套属性时,未授权的参数,has_many关联表单 [英] Unpermitted parameter in Rails 4 when submitted nested attributes, has_many associated form
问题描述
我有两个模型( lead
which has_many:quote_metals
and accepts_nested_attributes_for:quote_metals
和 quote_metal
其中 belongs_to:lead
)。
我试图将以表单提交的信息写入不同的数据表。应该只有一个前导和多个(没有确切数字)的quote_metals。我收到一个错误,说:不允许的参数:quote_metal
。
这里是参数:
由LeadsController处理#create as html
参数:{utf8=>✓,authenticity_token=> h5iBI7teODFFXRk91puydlr4UmC9EhU5aR9ul2D0u4STFp68GV0wOgb / I43Ukga2MHjLWg1ZnbPeLwuLmMxrkw ==,lead=> {name=>Biggie,email=>test@test.com,...,note=& asdf,quote_metal=> [{metal=>iron,weight=>1,unit=>pounds},{metal=& commit=>提交}
这是我的控制器:
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:methods,Lead.methods.map {| k,v | [k.humanize,k]},{},
class:form-control,:required =>真实%>
< / div>
< div class =form-group>
<%= f.label:notes%>
<%= f.text_area:note,class:form-control%>
< / div>
<%= render:partial => metalLead,local:{f:f}%>
<%= render:partial => metalLead,local:{f:f}%>
< div class =form-group text-center>
<%= f.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 =>真实%>
< / 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 =>真实%>
< / div>
<%end%>
领导模型正在写入其数据表。我做错了导致quote_metal不写?
解决方案错误在此行
<%= f.fields_for'quote_metal []' @quote_metal do | ff | %>
。应为<%= f.fields_for:quote_metals,@quote_metal do | ff | %>
I have two models (
lead
whichhas_many :quote_metals
andaccepts_nested_attributes_for :quote_metals
andquote_metal
whichbelongs_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?
解决方案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| %>
这篇关于在Rails 4中提交嵌套属性时,未授权的参数,has_many关联表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!