【问题标题】:polymorphic nested form active_admin rails多态嵌套形式 active_admin rails
【发布时间】:2013-08-14 02:00:52
【问题描述】:

我在网上寻找了很多解决方案,但似乎找不到答案。

我有一个表链接的多态关联,它链接到许多其他表。

这是我的模型有点简化:

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

我似乎找不到问题,这让我发疯了......

似乎缺少关系或其他东西,但我找不到它。

非常感谢每一位可以提供帮助的人!

【问题讨论】:

    标签: ruby-on-rails activerecord associations activeadmin polymorphic-associations


    【解决方案1】:

    我认为问题在于您命名模型的方式。模型总是被声明为单数实体,而不是复数。

    你应该:

    1. links.rb 重命名为link.rb
    2. events.rb 重命名为event.rb
    3. class Links &lt; ActiveRecord::Base 重命名为class Link &lt; ActiveRecord::Base

    看看有没有帮助。

    【讨论】:

    • 完美!我想我对我的代码太投入了,以至于我看不到它。原来我在写 events.rb 时犯了一个错误,它是单数的,但链接不是,现在它可以工作了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多