【问题标题】:Rails Double nested form is not workingRails 双嵌套形式不起作用
【发布时间】:2016-03-10 10:38:12
【问题描述】:

我正在开发一个应用程序,这是域模型的提取部分

CustomerPerson 的子类,如您所见,Address 是多态的:我的 domainmodel 中有更多模型使用这个模型。

这些是我的模型文件:

模型客户 文件:customer.rb

class Customer < Person

  has_many :homes
  accepts_nested_attributes_for :homes

  validates :dateOfBirth, date: true

end

模型 Home 文件: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' 可以呈现一个表单。这种形式是双重嵌套的。有 Customer 字段、Home 字段和 Address 字段。

文件:customers_controller.rb

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

这个方法渲染这个表单:

这是这个表单的代码

文件:_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

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

如果有人对我有建议,那就太好了

马丁·D.

【问题讨论】:

  • 你在控制器中调试过你的代码吗?在创建行动中,您的参数中有什么?嵌套属性的参数中是否存在地址?
  • 您能否指定addresses表架构?
  • 嗨,添加了地址表架构来发布..

标签: ruby-on-rails forms nested


【解决方案1】:

感谢制定我的问题并将其发布在这里,我更改了我的 params.require 和瞧……它现在可以工作了。

由于 HomeAddress 之间的关联是“has_one”,这也应该反映在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多