2016-04-29 94 views
0

强制要求保存关于多态关联的嵌套模型的帮助。我失去了一些东西,但无法弄清楚什么。永恒的相当直接。有地址可以有多个电话。 所以模型无法在多态关联上保存嵌套模型

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"}

我缺少什么? 2个问题

+0

1. http://stackoverflow.com/questions/4853373/rails-fields-for-with-index –

+0

2.通过PARAMS通不是'phone'。但是'phones_attributes'哈希数组 –

回答

0

尝试回答:

  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. 应该可以解决从第1个问题开始,由于字段shoudl通过params而不是phone散列发送ot服务器,但是phones_attributes散列数组,以便accepts_nested_attributes_for可以接受电话:

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

您节省了我的一天!现在一切正常。非常感谢。祝一切顺利! –