Rails双嵌套表单不起作用

时间:2016-03-10 10:38:13

标签: ruby-on-rails forms nested

我正在处理一个应用程序,这是域模型的提取部分 abstract domain model

客户的子类,您可以看到地址是多态的:我的域模型中有更多模型可供使用这个模型。

这些是我的模型文件:

模型客户 文件:customer.rb

class Customer < Person

  has_many :homes
  accepts_nested_attributes_for :homes

  validates :dateOfBirth, date: true

end

模型主页 文件:home.rb

class Home < ActiveRecord::Base

  belongs_to :customer
  has_one  :address, as: :x_address
  accepts_nested_attributes_for :address

end

地址 文件:address.rb

class Address < ActiveRecord::Base

  belongs_to :x_address, polymorphic: true

end

customers_controller.rb 中有一个呈现表单的方法'new'。此表单是双嵌套的。 客户的字段,主页的字段和地址的字段。

file:customers_controller.rb

def new
    @customer = Customer.new
    home = @customer.homes.build
    home.build_address
  end

此方法呈现此形式:

double nested form

这是此表格的代码

file:_form.html.erb

<%= form_for @customer, :url => { :action => "newCustomer" } do |f| %>

<%= render 'errors' %>


<fieldset class="form-group">

  <div class="">
    <span class="form-control-label"> EMAIL</span>
    <span><%= f.email_field :email , :class => "form-control" %></span>
  </div>

  <div class="">
    <span class="form-control-label">GESLACHT</span>
    <span><%= f.text_field :gender, :class => "form-control" %></span>
  </div>

  <div >
    <span class="form-control-label">GEBOORTEDATUM</span>
    <span class="form-control"><%= f.date_select  :dateOfBirth, :start_year => 1950  %></span>
  </div>



  <!--nesting formfields for homes and address-->

  <%= f.fields_for :homes do |home| %>
      <div class="">
        <span class="form-control-label">NAAM WONING</span>
        <span><%= home.text_field :name, :class => "form-control" %></span>
      </div>

          <%= home.fields_for :address do |adres| %>
              <div class="">
                <span class="form-control-label">STRAATNAAM</span>
                <span><%= adres.text_field :street, :class => "form-control" %></span>
              </div>
          <% end %>
  <% end %>



</fieldset>

<div class="input_row">
  <span class="input_label blank"></span>
  <span class="submit_button"><%= f.submit :value => "OPSLAAN"  %></span>
</div>
adresses schema:

 Table name: addresses

  id             :integer          not null, primary key

  street         :string
  homeNumber     :string
  postalCode     :string
  city           :string
  isPostAddress  :boolean
  x_address_id   :integer
  x_address_type :string
  created_at     :datetime         not null
  updated_at     :datetime         not null

这是我允许在Customers_controller中使用所有属性的方法:

    def customer_params
        params.require(:customer).permit( :email,
                                          :gender,
                                          :customerType,
                                          :dateOfBirth,
                                          :maritialStatus,
                                          :childrenCount,
                                          :partyMail,
                                          :marketingMail,
                                          homes_attributes: [:name,
                                                            addresses_attributes: [:street,
                                                                                 :homeNumber,
                                                                                 :postalCode,
                                                                                 :city,
                                                                                 :isPostaddress]])

end

我的问题是,通过提交此表单,它不会创建地址。所以它确实创建了一个客户和一个家庭,但似乎我在为地址做错了。我不知道是什么。我已经搜索过解决方案,但找不到我做错了什么。

如果有人对我提出建议会很棒

Martijn D。

1 个答案:

答案 0 :(得分:0)

感谢制定我的问题在这里发布,我改变了我的params.require和Voila ......它现在有效。

由于主页地址之间的关联是&#39; has_one&#39;这也应该反映在params.require中......

所以我将 addresses_attributes更改为address_atributes

 def customer_params
        params.require(:customer).permit( :email,
                                          :gender,
                                          :customerType,
                                          :dateOfBirth,
                                          :maritialStatus,
                                          :childrenCount,
                                          :partyMail,
                                          :marketingMail,
                                          homes_attributes: [:name,
                                                            address_attributes: [:street,
                                                                                 :homeNumber,
                                                                                 :postalCode,
                                                                                 :city,
                                                                                 :isPostaddress]])

end