无法在多态关联上保存嵌套模型

时间:2016-04-29 09:08:29

标签: ruby-on-rails polymorphic-associations

强制要求帮助保存嵌套模型的多态关联。我错过了什么,但无法弄清楚是什么。 Everithing非常直截了当。有可以有多个电话的地址。 所以型号是

class Address < ActiveRecord::Base      
  has_many :phones, as: :phoneable  
  accepts_nested_attributes_for :phones, allow_destroy: true  
  validates :city, :street, :building, :name, presence: true  
end

class Phone < ActiveRecord::Base  
  belongs_to :phoneable, polymorphic: true  
  validates :number, :extension, presence: true        
end

addresses_controller.rb

def new
  @address = Address.new
  @phone = @address.phones.build
  authorize @address
end

def create
  @address = Address.create(address_params)
  authorize @address
  if @address.save
    binding.pry
    flash[:success] = "Address #{@address.name} created"
    redirect_to address_path(@address)
  else
    flash.now[:danger] = 'Failed'
    render :new
  end
end

 def address_params
     params.require(:address).permit(:name, :street, :building, :city, phones_attributes: [:id, :number, :extension, :details] )
 end

/app/views/address.html.erb

    <div class="row">
      <div class="col-md-12">
        <%= form_for(@address, html: {class: 'form-horizontal', role: 'form'}) do |f| %>
          <%= render 'shared/errors', obj: @address, model_name: 'addresses' %>
          <div id="create-form">
            <div class="form-group">
              <div class="control-label col-md-4">
                <%= f.label :city, 'Город' %>
              </div>
              <div class="col-md-4">
                <%= f.select(:city, options_for_select(['Moscow', 'Samara']), {}, {class: "form-control"}) %>
              </div>
            </div>

            <div class="form-group">
              <div class="control-label col-md-4">
                <%= f.label :street, 'Street' %>
              </div>
              <div class="col-md-4">
                <%= f.text_field :street, class: 'form-control' %>
              </div>
            </div>

            <div class="form-group">
              <div class="control-label col-md-4">
                <%= f.label :building, 'Building' %>
              </div>
              <div class="col-md-4">
                <%= f.text_field :building, class: 'form-control' %>
              </div>
            </div>

            <div class="form-group">
              <div class="control-label col-md-4">
                <%= f.label :name, 'Place name' %>
              </div>
              <div class="col-md-4">
                <%= f.text_field :name, class: 'form-control' %>
              </div>
            </div>

            <div class="form-group">
              <div class="control-label col-md-4">
                <%= f.label :phones, 'Phone' %>
              </div>
              <div class="col-md-4">
                <%= f.fields_for :phone do |phone_form| %>
                  <%= phone_form.text_field :number, class: 'form-control' %>
              </div>
            </div>

            <div class="form-group">
              <div class="control-label col-md-4">
                <%= phone_form.label :extension, 'Ext.' %>
              </div>
              <div class="col-md-4">
                <%= phone_form.text_field :extension, class: 'form-control' %>
              </div>
                <% end %>
            </div>

            <div class="form-group">
              <div class='col-md-offset-4 col-md-6'>
                <%= f.submit @address.new_record? ? 'Add' : 'Update', class: 'btn btn-primary btn-md' %>
              </div>
            </div>
          </div>
        <% end %>
      </div>
    </div>

我遇到的第一个问题是,如果我将密钥:phones而不是:phone设置到以下行<%= f.fields_for :phone do |phone_form| %>,我的手机文本字段不会在视图中呈现,但它们应该。一位用户在此强调了这一时刻https://stackoverflow.com/a/3328041/2049476 如果我使用:phone一切都工作正常,但似乎是错的。 第二个。 电话对象不保存在数据库中,当我创建新地址或编辑当前我成功但如果我将所有字段留空,则电话不会显示任何验证错误。 这就是我在params哈希

中的内容

{"utf8"=>"✓", "authenticity_token"=>"inwXr3Ev/Aj/hZRY2IadizDHDgdSFo2zFhY9DAvysfFu3jjD9AS66esKVsTzEuKo2WC46YQt6HnOKTgInvfUEg==", "address"=>{"city"=>"Moscow", "street"=>"ul. Tsentralnaya d. 4 kv. 220", "building"=>"1212", "name"=>"Astoria", "phone"=>{"number"=>"9215555555", "extension"=>"111"}}, "commit"=>"Add", "controller"=>"addresses", "action"=>"create"}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

尝试回答2个问题:

  1. 正确的方法是将:phones,然后phone作为变量传递给field_for,就像已完成here一样:

    <%- @address.phones.each.with_index do |phone, index| %>
       <%- f.fields_for :phones, phone do |phone_form| %>  
    
       <%- end %>
    <%- end %>
    
  2. 应该从第一个问题开始解决,因为shoudl通过params发送服务器而不是phone哈希,而是phones_attributes哈希数组,命令accepts_nested_attributes_for可以接受电话:

    phones_attributes: [{ ... },{ ... }]