0

我一直在尝试输入嵌套属性到我的数据库,但我无法设法得到它的工作。Rails嵌套属性未找到/不允许

我有这些参数:

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 

这些输入PARAMS:

{"utf8"=>"✓", 
"authenticity_token"=>"P4WTxAHKLgeRUQq60JvD/uMjo0s2BdMtqlBr1B6Z2hQ=", 
"domain"=>{"domain_name"=>"test.com", 
"whois_contacts_attributes"=>{"0"=>{"first_name"=>"Edward", 
"last_name"=>"Doe", 
"street"=>"Broadway", 
"number"=>"1", 
"postal_code"=>"1234AB", 
"city"=>"Amsterdam", 
"country_id"=>"205", 
"phone_number"=>"316123465", 
"email_address"=>"[email protected]", 
"company_name"=>"", 
"company_kvk"=>"", 
"company_type_id"=>""}}, 
"nameserver_first"=>"ns1.dns.com", 
"nameserver_second"=>"ns2.dns.com"}, 
"commit"=>"Register domain"} 

我得到象这样的错误: param not found: whois_contacts_attributes

用Google搜索大量的示例,但它不起作用。

在我的模型,我有:

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: "[email protected]" } %> 

    <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 %> 
+0

我要去出门在外,责备你的参数。我真的很喜欢Rails 4移动到的白名单,但他们可能会让界面变得更友好。我已经挖掘了它,回答了有关它的问题,并且我仍然不明白它是如何工作的。看看我在这里给出的答案:http://stackoverflow.com/questions/19189602/unpermitted-parameters-for-dynamic-forms-in-rails-4/19194274#19194274我认为你的问题是相反的。你正在编写'permit'语句中的一个数组,但是你的params中会嵌套散列值。 – Beartech

+0

我在想''whois_contacts_attributes“=> {”0“=> {”first_name“=>”Edward“,'应该返回更类似于:'”whois_contacts_attributes“=> {”first_name“=>”Edward ” ...'。这指出了我的第二个弱点,即simple_fields_for API。对我来说这是一个黑匣子,所以我不太懂得如何获得我想要的参数。你可以对该表单执行一个控制器操作,只是在视图中打印表单返回的参数?这就是我过去追捕这些参数的奥秘所在。 – Beartech

回答

2

有3两件事我会做出不同。

1建立你的控制器记录

def new 
    @domain = Domain.new 
    @domain.whois_contacts.build 
end 

2在同一个方法调用的白名单

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 

3调整形式更好地建设语句

<%= 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: "[email protected]" } %> 

    <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 %> 
+0

感谢您的回复。这会产生错误'unknown attribute:domain_name'不幸的是 – edwardmp

+0

所以我建立了一个虚拟项目和我所拥有的信息(显然减去表单的域部分和你的控制器的其余部分),并且我没有得到那个错误。您可以查看这些差异并查看是否可以识别您的问题(有几处更改,company_kvm被偶然忽略,而country和company_type使用name而不是full_name,因此请小心使用剪切和粘贴)https:// github .com/trh/rails-nested-fieldsfor-example – trh

+0

嘿谢谢你的所有努力。我实际上需要构建3次(对于三种类型的联系人),现在我成功了。所有这些联系人现在立即创建,这非常棒!唯一剩下的就是在whois_contacts中我有一个user_id列,它还没有被设置。我可以把它变成一个隐藏的领域,但从安全的角度来看这不是一个好的做法。有任何想法吗? – edwardmp