【问题标题】:Rails nested attributes not found / not permittedRails 嵌套属性未找到/不允许
【发布时间】:2013-12-09 00:41:10
【问题描述】:

我一直在尝试将嵌套属性输入到我的数据库中,但无法使其正常工作。

我有这些参数:

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

这些输入参数:

{"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@email.com",
 "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

用谷歌搜索了很多例子,但没有用。

在我的模型中,我有:

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

【问题讨论】:

  • 我要四处走动并责怪你的参数。我真的很喜欢 Rails 4 迁移到的参数白名单,但它们本可以使白名单的界面更加用户友好。我已经深入研究了它,回答了有关它的问题,但我仍然不真正了解它是如何工作的。看看我在这里给出的答案:stackoverflow.com/questions/19189602/… 我认为你的问题是相反的。您正在为 permit 语句中的数组编码,但您的参数中得到了嵌套的哈希值。
  • 我在想"whois_contacts_attributes"=&gt;{"0"=&gt;{"first_name"=&gt;"Edward", 应该返回更像:"whois_contacts_attributes"=&gt; {"first_name"=&gt;"Edward",...。这指出了我的第二个弱点,simple_fields_for API。这对我来说有点像一个黑匣子,所以我真的不明白如何从中获得我想要的参数。您可以为该表单执行一个控制器操作,该操作仅在视图中打印出表单返回的参数吗?这就是我过去寻找这些参数奥秘的方式。

标签: ruby-on-rails ruby ruby-on-rails-4 nested-forms nested-attributes


【解决方案1】:

有 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: "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 %>

【讨论】:

  • 感谢您的回复。不幸的是,这会产生错误unknown attribute: domain_name
  • 所以我使用我拥有的信息设置了一个虚拟项目(显然减去了表单的域部分和控制器的其余部分)并且我没有收到该错误。您可以查看差异,看看您是否可以确定您的问题(有一些更改,company_kvm 被意外省略了,并且 country 和 company_type 使用的是 name 而不是 full_name 所以要小心剪切和粘贴)github.com/trh/rails-nested-fieldsfor-example跨度>
  • 嘿,感谢您的所有努力。我实际上需要构建 3 次(用于三种类型的联系人),现在我成功了。现在立即创建所有这些联系人,这太棒了!唯一剩下的是,在 whois_contacts 我有一列 user_id 尚未设置。我可以将其设置为表单中的隐藏字段,但从安全角度来看,这不是一个好的做法。有什么想法吗?
  • 我会在您创建时将其设置在控制器中(或更新,因为更新可能会添加新的嵌套记录)@domain = Domain.new(domain_params.merge(user_id: current_user.id) 或类似的东西
  • 是的,我在某处读到过。但它不在域表中,而是在嵌套属性表中,因此合并不会那样工作。现在通过在插入后循环遍历他的联系人并手动更新它来修复它。
猜你喜欢
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多