強制要求保存關於多態關聯的嵌套模型的幫助。我失去了一些東西,但無法弄清楚什麼。永恆的相當直接。有地址可以有多個電話。 所以模型無法在多態關聯上保存嵌套模型
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個問題
1. http://stackoverflow.com/questions/4853373/rails-fields-for-with-index –
2.通過PARAMS通不是'phone'。但是'phones_attributes'哈希數組 –