我在网上寻找了许多解决方案,但似乎找不到答案。
我有一个链接到许多其他表的表链接的多态关联。
这是我的模型有点简化:
links.rb
class Links < ActiveRecord::Base
belongs_to :linkable, polymorphic: true
end
events.rb
class Event < ActiveRecord::Base
has_many :links, as: :linkable
accepts_nested_attributes_for :links
end
这是管理员表格
events.rb
ActiveAdmin.register Event do
form do |f|
f.has_many :links do |link_f|
link_f.inputs "links" do
link_f.input :url
end
end
f.actions
end
end
这是我的schema.rb中的内容
create_table "links", force: true do |t|
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "linkable_id"
t.string "linkable_type"
end
它引发了我这样的错误: 未初始化的常量Event :: Link
我似乎找不到问题,这让我发疯了...
似乎缺少某种关系或某些东西,但我找不到它。
非常感谢您能提供的所有帮助!
我认为问题出在您命名模型的方式上。 模型总是声明为单数实体,而不是复数实体。
你应该:
links.rb
重命名为link.rb
events.rb
为event.rb
class Links < ActiveRecord::Base
到class Link < ActiveRecord::Base
看看是否有帮助。