Ruby on rails 嵌套参数,具有一对一多态性

Ruby on rails 嵌套参数,具有一对一多态性,ruby-on-rails,ruby-on-rails-4,nested-attributes,Ruby On Rails,Ruby On Rails 4,Nested Attributes,我遇到了一个问题,我正试图使用我的#new-操作中的一个表单向某人添加地址,与往常一样。 我现在正在做的是 class Person < ActiveRecord::Base has_one :address, :as => :owner accepts_nested_attributes_for :address end class Address < ActiveRecord::Base belongs_to :owner, :polymorphic =&g

我遇到了一个问题,我正试图使用我的
#new
-操作中的一个表单向某人添加地址,与往常一样。
我现在正在做的是

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
提交表格时,使用以下参数:

{"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 %>

“地址1”%>
“地址2”%>
“表单控件”%>
“表单控件”%>

让我们尝试一件事。请删除此行并重新启动服务器

# config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|'
  inflect.irregular 'address', 'addresses'
end
编辑
#我们在聊天中详细讨论了这个问题后,找到了解决方案。
#对象“Person”未正确初始化为f.fields\u for:address
#看法
#添加这一行

了解您在哪一行收到此错误会很有帮助。哦,是的,没错。在
Person.new
-行上非常奇怪
接受
的{u嵌套的{u属性}和的f字段{u:address应将您的参数转换为这样的…
“person”=>{“name”=>“mr man”,“address\u属性”=>{“line\u one”=>“test address 15”
没错。我不太明白为什么没有。我唯一的想法是,我使用的是从地址到所有者的多态关系,而此人就是所有者。@Brenoperucchit这不是问题。我在这里使用相同的实现没有成功,很遗憾。我得到了相同的例外。您的实现与我的实现唯一不同的是名称类
地址。rb
和我的has\u many是这样的
has\u many:addresses,class\u name:'addresses',as::addressable
我在另一个类中也有相同的。User.rb有一个:addresses…我仍然认为问题是屈折。编辑你的帖子并显示视图添加了OPYour view中的视图是正确的。在你的 拐点.rb
拐点.不可数的“地址”
并注释另一行
# 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| %>