简体   繁体   中英

polymorphic nested form active_admin rails

I've looked for many solutions on the web and I can't seem to find my answer.

I have a polymorphic association for a table links that it linked to many other tables.

Here is my models a bit simplified:

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

here is the admin form

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

Here's what in my 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

It throws me that error: uninitialized constant Event::Link

I can't seem to find the problem and it is driving me nuts...

It seems like a relation is missing or something but I can't find it.

Thanks a lot for every one that can help!

I think the problem is in the way you named your models. Models are always declared as singular entities, not plural.

You should:

  1. Rename links.rb to link.rb
  2. Rename events.rb to event.rb
  3. Rename class Links < ActiveRecord::Base to class Link < ActiveRecord::Base

and see if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM