嵌套参数,具有一对一的多态性
Nested Parameters, with polymorphic one-to-one
我遇到了一个问题,我尝试像往常一样在我的 #new
-action 中使用表单向一个人添加地址。
我目前正在做的是
class Person < ActiveRecord::Base
has_one :address, :as => :owner
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
end
在我的表单中,我使用 form_for @person do |f|
- 然后是表单,然后是 f.fields_for :address do |af|
和字段,使用 af.text_field
.
我的控制器操作:
def create
person = Person.new(person_params)
if person.valid?
person.save
redirect_to edit_content_person_path(@current_work_context, person)
else
@person = person
render 'new'
end
end
def person_params
params.require(:person).permit( :name, :address => [:line_one, :line_two, :zipcode, :city])
end
当我使用这些参数提交表单时:
{"utf8"=>"✓",
"authenticity_token"=>"blablabla==",
"person"=>{"name"=>"mr man",
"address"=>{"line_one"=>"test address 15",
"line_two"=>"",
"zipcode"=>"2600",
"city"=>"Glostrup"}},
"locale"=>"da",
"context_id"=>"9"}
然而,我得到了异常:ActiveRecord::AssociationTypeMismatch
Address(#70198415678880) expected, got ActionController::Parameters(#70198414055220)
我似乎不明白为什么。
编辑 - 我的观点,简化:
<%= form_for @person do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :address do |af| %>
<%= af.label :address %>
<%= af.text_field :line_one, :placeholder => "Address 1" %>
<%= af.text_field :line_two, :placeholder => "Address 2" %>
<%= af.label :zipcode %>
<%= af.text_field :zipcode, :class => "form-control" %>
<%= af.label :city %>
<%= af.text_field :city, :class => "form-control" %>
<% end %>
<% end %>
让我们尝试一件事。请和这行并重新启动你的服务器
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|'
inflect.irregular 'address', 'addresses'
end
编辑
# after we discussing more about the problem in chat we found the solution.
# The object 'Person' not initialize correctly to f.fields_for :address
# view
<% f.object.build_address if f.object.address.blank? %> #add this line
<%= f.fields_for :address do |af| %>
我遇到了一个问题,我尝试像往常一样在我的 #new
-action 中使用表单向一个人添加地址。
我目前正在做的是
class Person < ActiveRecord::Base
has_one :address, :as => :owner
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
end
在我的表单中,我使用 form_for @person do |f|
- 然后是表单,然后是 f.fields_for :address do |af|
和字段,使用 af.text_field
.
我的控制器操作:
def create
person = Person.new(person_params)
if person.valid?
person.save
redirect_to edit_content_person_path(@current_work_context, person)
else
@person = person
render 'new'
end
end
def person_params
params.require(:person).permit( :name, :address => [:line_one, :line_two, :zipcode, :city])
end
当我使用这些参数提交表单时:
{"utf8"=>"✓",
"authenticity_token"=>"blablabla==",
"person"=>{"name"=>"mr man",
"address"=>{"line_one"=>"test address 15",
"line_two"=>"",
"zipcode"=>"2600",
"city"=>"Glostrup"}},
"locale"=>"da",
"context_id"=>"9"}
然而,我得到了异常:ActiveRecord::AssociationTypeMismatch
Address(#70198415678880) expected, got ActionController::Parameters(#70198414055220)
我似乎不明白为什么。
编辑 - 我的观点,简化:
<%= form_for @person do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :address do |af| %>
<%= af.label :address %>
<%= af.text_field :line_one, :placeholder => "Address 1" %>
<%= af.text_field :line_two, :placeholder => "Address 2" %>
<%= af.label :zipcode %>
<%= af.text_field :zipcode, :class => "form-control" %>
<%= af.label :city %>
<%= af.text_field :city, :class => "form-control" %>
<% end %>
<% end %>
让我们尝试一件事。请和这行并重新启动你的服务器
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|'
inflect.irregular 'address', 'addresses'
end
编辑
# after we discussing more about the problem in chat we found the solution.
# The object 'Person' not initialize correctly to f.fields_for :address
# view
<% f.object.build_address if f.object.address.blank? %> #add this line
<%= f.fields_for :address do |af| %>