在我尝试做的工作中,使用nested_form,祝您愉快
#_form.html.erb in post
<%= f.fields_for :addresses do |address| %>
<% address.association :state %>
<% end %>
class Post < ApplicationRecord
has_many :addresses, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :addresses, allow_destroy: true
end
class Address < ApplicationRecord
belongs_to :states
belongs_to :addressable, :polymorphic => true
end
class State < ApplicationRecord
has_many :addresses
end打印错误:关联:状态未找到!帮帮我!
发布于 2016-08-06 15:23:12
来自文档
类书< ApplicationRecord belongs_to :author belongs_to关联必须使用单数术语。如果您在上述示例中为Book模型中的author关联使用了复数形式,您将被告知存在一个“未初始化的常量Book::Authors”。这是因为Rails从关联名称中自动推断类名。如果关联名是错误的多元化,那么推断类也会被错误地多元化。
所以你必须替换这条线:
belongs_to :states使用
belongs_to :state发布于 2016-08-06 15:00:35
更改State类代码。
class Address < ApplicationRecord
belongs_to :state
belongs_to :addressable, :polymorphic => true
end您使用的是states而不是state。因此,您将得到错误。
https://stackoverflow.com/questions/38805491
复制相似问题