[英]Can't save nested model on polymorphic association
Forced to ask for help with saving nested models on polymorphic association. 被迫寻求有关在多态关联上保存嵌套模型的帮助。 I'm missing something but can't figure out what. 我缺少了一些东西,但找不到。 Everithing pretty straightforward. 一切都非常简单。 There is Address which can have multiple Phones. 有可以有多个电话的地址。 So models are 所以模型是
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
and 和
class Phone < ActiveRecord::Base
belongs_to :phoneable, polymorphic: true
validates :number, :extension, presence: true
end
addresses_controller.rb 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 /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>
First issue I encountered with is if I set key :phones
instead :phone
into the following line <%= f.fields_for :phone do |phone_form| %>
我遇到的第一个问题是,如果我将key :phones
代替:phone
放入以下行<%= f.fields_for :phone do |phone_form| %>
<%= f.fields_for :phone do |phone_form| %>
my phone text fields don't render in view but they should. <%= f.fields_for :phone do |phone_form| %>
我的电话文本字段没有显示,但应该显示。 One user emphasized this moment here https://stackoverflow.com/a/3328041/2049476 if I use :phone
everything somehow works fine but seems like it's wrong. 如果我使用:phone
一位用户在这里强调了这一时刻https://stackoverflow.com/a/3328041/2049476一切都可以正常工作,但似乎错了。 And the second one. 还有第二个。 Phone object doesn't save in DB, when I create new address or edit current I succeed but phone doesn't show any validation errors if I leave all it fields blank. 电话对象未保存在数据库中,当我创建新地址或编辑当前地址时,我成功了,但是如果我将所有字段保留为空白,电话不会显示任何验证错误。 Here what I have in params
hash 这是我在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"}
What am I missing? 我想念什么?
Try answer for 2 issues: 尝试回答2个问题:
The correct way is to pass :phones
, and then phone
as variable to field_for
, like is was done here : 正确的方法是传递:phones
,然后将phone
作为变量传递给field_for
,就像在这里完成的那样:
<%- @address.phones.each.with_index do |phone, index| %> <%- f.fields_for :phones, phone do |phone_form| %> <%- end %> <%- end %>
Should be resolved as of the 1-st question, since the fields shoudl sent ot server via params
not a phone
hash, but phones_attributes
array of hashes, in order to accepts_nested_attributes_for
could accept phones: 应该从第一个问题开始解决,因为shoudl字段是通过params
发送给ot服务器的,而不是通过params
发送的,不是通过phone
散列,而是通过phones_attributes
哈希数组,以便accepts_nested_attributes_for
可以接受电话:
phones_attributes: [{ ... },{ ... }]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.