Ruby on rails 未找到/不允许Rails嵌套属性
我一直试图将嵌套属性输入到数据库中,但无法使其正常工作 我有以下参数:Ruby on rails 未找到/不允许Rails嵌套属性,ruby-on-rails,ruby,ruby-on-rails-4,nested-forms,nested-attributes,Ruby On Rails,Ruby,Ruby On Rails 4,Nested Forms,Nested Attributes,我一直试图将嵌套属性输入到数据库中,但无法使其正常工作 我有以下参数: def domain_register_whois_contact_parameters params.require(:domain).permit( whois_contacts_attributes: [:id, :first_name, :last_name, :street, :number, :postal_code, :city, :phone_number, :email_address, :compan
def domain_register_whois_contact_parameters
params.require(:domain).permit( whois_contacts_attributes: [:id, :first_name, :last_name, :street, :number, :postal_code, :city, :phone_number, :email_address, :company_type_id, :company_kvk, :company_name, :country_id])
end
以下是输入参数:
我遇到如下错误:
未找到参数:谁是联系人属性
谷歌搜索了很多例子,但都不起作用
在我的模型中,我有:
accepts_nested_attributes_for :whois_contacts
我的看法是:
<%= f.simple_fields_for :whois_contacts, @domain.whois_contacts.new do |wc| %>
<%= wc.input :first_name, input_html: { placeholder: "John" } %>
<%= wc.input :last_name, input_html: { placeholder: "Doe" } %>
<%= wc.input :street, input_html: { placeholder: "Main street" } %>
<%= wc.input :number, input_html: { placeholder: "1" } %>
<%= wc.input :postal_code, input_html: { placeholder: "1234AB" } %>
<%= wc.input :city, input_html: { placeholder: "Amsterdam" } %>
<div class="form-group select required domain_country_country_name">
<label class="select required form-label" for="domain_country_country_name">
<abbr title="required">*</abbr> Country
</label>
<div class="controls">
<%= wc.collection_select :country_id, Country.all, :id, :full_name, {}, {:class=>'select required form-control'} %>
</div>
</div>
<%= wc.input :phone_number, input_html: { placeholder: "0612345678" } %>
<%= wc.input :email_address, input_html: { placeholder: "doe@johndoe.com" } %>
<h3>Company information</h3>
<%= wc.input :company_name, hint: 'If you have a company, please enter the company name.', input_html: { placeholder: "Doe Inc." } %>
<%= wc.input :company_kvk, hint: 'Enter Chamber of Commerce (Kvk) number', input_html: { placeholder: "12345678" } %>
<div class="form-group select required domain_company_company_name">
<label class="select required form-label" for="domain_company_companyname">
<abbr title="required">*</abbr> Company
</label>
<div class="controls">
<%= wc.collection_select :company_type_id, CompanyType.all, :id, :full_name, {}, {:class=>'select required form-control'} %>
</div>
</div>
<% end %>
*国家
“选择所需的表单控件”}%>
公司信息
*公司
“选择所需的表单控件”}%>
有三件事我会做得不同
1在控制器中建立记录
同一方法调用上的2个白名单
3调整表单以获得更好的构建语句
*国家
“选择所需的表单控件”}%>
公司信息
*公司
“选择所需的表单控件”}%>
我要冒险去责备你的情妇。我真的很喜欢Rails 4移动到的参数的白名单,但是它们可以使白名单的界面更加用户友好。我已经深入研究了它,回答了有关它的问题,但我仍然不能真正理解它是如何工作的。看看我在这里给出的答案:我认为你的问题恰恰相反。您正在为permit
语句中的数组编码,但在参数中得到嵌套哈希。我认为“whois\u contacts\u attributes”=>{“0”=>{“first\u name”=>“Edward”
应该以类似的形式返回:“whois\u contacts\u attributes”=>{“first\u name”=>“Edward”,…
。这指出了我的第二个弱点,即API的简单字段。这对我来说有点像一个黑匣子,所以我真的不知道如何从中获取我想要的参数。在视图中,你能为该表单执行一个控制器操作,只打印表单返回的参数吗?这就是我如何在t中找到这些参数的奥秘他过去了。谢谢你的回复。这会产生错误未知属性:domain\u name
不幸的是,我用我所拥有的信息(减去表单的域部分和你控制器的其余部分)设置了一个虚拟项目,但我没有发现这个错误。你可以看看它们的区别,看看你是否能识别出你的问题(有一些更改,company_kvm被意外省略,country和company_type使用的是名称而不是全名,因此请小心剪切和粘贴)嘿,谢谢你的所有努力。我实际上需要构建3次(对于三种类型的联系人)我现在成功了。所有这些联系人现在都立即创建了,这很好!唯一剩下的是,在whois_contacts中,我有一个尚未设置的列user_id。我可以将其设置为表单中的隐藏字段,但从安全角度看,这不是一个好的做法。有什么想法吗?我会在创建联系人时在控制器中设置它(或者更新为更新可能会添加新的嵌套记录)@domain=domain.new(domain\u params.merge(user\u id:current\u user.id)
或类似的内容是的,我在某个地方读到过。但它不在domains表中,而是在nested attribute选项卡中,因此merge不会以这种方式工作。现在通过在插入后循环使用联系人并手动更新来修复它。
<%= f.simple_fields_for :whois_contacts, @domain.whois_contacts.new do |wc| %>
<%= wc.input :first_name, input_html: { placeholder: "John" } %>
<%= wc.input :last_name, input_html: { placeholder: "Doe" } %>
<%= wc.input :street, input_html: { placeholder: "Main street" } %>
<%= wc.input :number, input_html: { placeholder: "1" } %>
<%= wc.input :postal_code, input_html: { placeholder: "1234AB" } %>
<%= wc.input :city, input_html: { placeholder: "Amsterdam" } %>
<div class="form-group select required domain_country_country_name">
<label class="select required form-label" for="domain_country_country_name">
<abbr title="required">*</abbr> Country
</label>
<div class="controls">
<%= wc.collection_select :country_id, Country.all, :id, :full_name, {}, {:class=>'select required form-control'} %>
</div>
</div>
<%= wc.input :phone_number, input_html: { placeholder: "0612345678" } %>
<%= wc.input :email_address, input_html: { placeholder: "doe@johndoe.com" } %>
<h3>Company information</h3>
<%= wc.input :company_name, hint: 'If you have a company, please enter the company name.', input_html: { placeholder: "Doe Inc." } %>
<%= wc.input :company_kvk, hint: 'Enter Chamber of Commerce (Kvk) number', input_html: { placeholder: "12345678" } %>
<div class="form-group select required domain_company_company_name">
<label class="select required form-label" for="domain_company_companyname">
<abbr title="required">*</abbr> Company
</label>
<div class="controls">
<%= wc.collection_select :company_type_id, CompanyType.all, :id, :full_name, {}, {:class=>'select required form-control'} %>
</div>
</div>
<% end %>
def new
@domain = Domain.new
@domain.whois_contacts.build
end
def domain_params
params.require(:domain).permit(:domain_name, :nameserver_first, :nameserver_second, whois_contacts_attributes: [:id, :first_name, :last_name, :street, :number, :postal_code, :city, :phone_number, :email_address, :company_type_id, :company_kvk, :company_name, :country_id])
end
<%= f.simple_fields_for :whois_contacts do |wc| %>
<%= wc.input :first_name, input_html: { placeholder: "John" } %>
<%= wc.input :last_name, input_html: { placeholder: "Doe" } %>
<%= wc.input :street, input_html: { placeholder: "Main street" } %>
<%= wc.input :number, input_html: { placeholder: "1" } %>
<%= wc.input :postal_code, input_html: { placeholder: "1234AB" } %>
<%= wc.input :city, input_html: { placeholder: "Amsterdam" } %>
<div class="form-group select required domain_country_country_name">
<label class="select required form-label" for="domain_country_country_name">
<abbr title="required">*</abbr> Country
</label>
<div class="controls">
<%= wc.collection_select :country_id, Country.all, :id, :full_name, {}, {:class=>'select required form-control'} %>
</div>
</div>
<%= wc.input :phone_number, input_html: { placeholder: "0612345678" } %>
<%= wc.input :email_address, input_html: { placeholder: "doe@johndoe.com" } %>
<h3>Company information</h3>
<%= wc.input :company_name, hint: 'If you have a company, please enter the company name.', input_html: { placeholder: "Doe Inc." } %>
<%= wc.input :company_kvk, hint: 'Enter Chamber of Commerce (Kvk) number', input_html: { placeholder: "12345678" } %>
<div class="form-group select required domain_company_company_name">
<label class="select required form-label" for="domain_company_companyname">
<abbr title="required">*</abbr> Company
</label>
<div class="controls">
<%= wc.collection_select :company_type_id, CompanyType.all, :id, :full_name, {}, {:class=>'select required form-control'} %>
</div>
</div>
<% end %>