LMo
LMo

Reputation: 1429

Active Admin has_many through table not rendering in form - have tried everything

Using Rails 3.2 and having a lot of trouble with Active Admin's has_many :through. The goal is to pull in the has_many :through subcategories through the contents model and create checkboxes for all of the subcategories. I've read the Active Admin documentation, searched other articles on stack, github, etc. Nothing is working. My only guess there could be something with the "form :html => { :enctype => "multipart/form-data" } do |f|" tag, which I need for PaperClip.

*I've tried using accepts_nested_attributes and it doesn't work...

There is a has_many :through table for subcategories and contents. Here is what it looks likes in the database:

create_table "contents", :force => true do |t|
  t.string   "title"
  t.text     "short_description"
  t.text     "long_description"
  t.datetime "published_date"
  t.datetime "edited_date"
  t.string   "read_length_time"
  t.string   "tag"
  t.integer  "author_id"
  t.integer  "contenttype_id"
  t.string   "collection"
  t.integer  "collection_id"
  t.string   "subcategory"
  t.integer  "subcategory_id"
  t.boolean  "published"
  t.datetime "created_at",               :null => false
  t.datetime "updated_at",               :null => false
  t.string   "image_file_name"
  t.string   "image_content_type"
  t.integer  "image_file_size"
  t.datetime "image_updated_at"
  t.string   "infographic_file_name"
  t.string   "infographic_content_type"
  t.integer  "infographic_file_size"
  t.datetime "infographic_updated_at"
  t.string   "video"
end

create_table "subcategories", :force => true do |t|
  t.string   "title"
  t.integer  "category_id"
  t.string   "content"
  t.integer  "content_id"
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
end

create_table "subcats_contents", :id => false, :force => true do |t|
  t.integer "subcategory_id"
  t.integer "content_id"
end

add_index "subcats_contents", ["content_id"], :name => "index_subcats_contents_on_content_id"
add_index "subcats_contents", ["subcategory_id", "content_id"], :name => "index_subcats_contents_on_subcategory_id_and_content_id"
add_index "subcats_contents", ["subcategory_id"], :name => "index_subcats_contents_on_subcategory_id"

Here are the models:

class Content < ActiveRecord::Base
  attr_accessible :author,
                  :edited_date,
                  :image,
                  :long_description,
                  :published_date,
                  :read_length_time,
                  :short_description,
                  :tag,
                  :title,
                  :type,
                  :collection_id,
                  :collection,
                  :collections_content,
                  :image,
                  :author_id,
                  :contenttype_id,
                  :subcategory,
                  :image_file_name,
                  :image_content_type,
                  :image_file_size,
                  :image_updated_at,
                  :subcats_contents,
                  :published,
                  :infographic_file_name,
                  :infographic_content_type,
                  :infographic_file_size,
                  :infographic_updated_at,
                  :infographic,
                  :video

  has_many :collections_contents
  has_many :collections, :through => :collections_contents

  has_many :subcats_contents
  has_many :subcategories, :through => :subcats_contents

  belongs_to :author
  belongs_to :contenttype

  validates_presence_of :title
  validates_presence_of :author_id
  validates_presence_of :published_date
  validates_presence_of :read_length_time
  validates_presence_of :contenttype
  validates_presence_of :tag
  validates :published, inclusion: [true, false]

  has_attached_file :image, :styles => { :medium => "500x800>", :thumb =>"500x500>" }
  has_attached_file :infographic, :styles => { :medium => "1000x1000>", :thumb =>"300x300>" }

  scope :published, where(:published => true )
  scope :unpublished, where(:published => false )


end

class Subcategory < ActiveRecord::Base
  attr_accessible :category,
                  :category_id,
                  :content,
                  :content_id,
                  :title,
                  :subcats_contents

  has_many :subcats_contents
  has_many :contents, :through => :subcats_contents

  belongs_to :category

end

class SubcatsContent < ActiveRecord::Base
  attr_accessible :subcategory,
                  :content

  belongs_to :subcategory
  belongs_to :content

end

Then in my active admin form I've tried everything... here is the current state:

ActiveAdmin.register Content do
  scope :all, :default => true
  scope :published
  scope :unpublished

  filter :author
  filter :contenttype, :label => "Content Type"
  filter :title
  filter :short_description, :label => "Summary"
  filter :long_description, :label => "Content Body"
  filter :published_date
  filter :tag, :label => "Tags"
  filter :subcategory, :label => "Subcategories"
  filter :image_file_name

  index do
    column :published
    column :title
    column :author
    column :published_date
    column :short_description, :label => "Summary"
    column :long_description, :label => "Content Body"
    column :image_file_name, :label => "Image"
    column :tag
    actions
  end

  form :html => { :enctype => "multipart/form-data" } do |f|
     f.inputs "Contents" do
      f.input :published, :as => :select
      f.input :title
      f.input :short_description, :label => "Summary"
      f.input :long_description, :label => "Content Body"
      f.input :read_length_time, :label => "Estimated Read Length Time"
      f.input :author
      f.input :contenttype, :label => "Type of Content"
      f.input :image, :as => :file
      f.input :infographic, :as => :file
      f.input :video, :label => "Video * Please add the embed code (not just the URL)"
      f.input :tag, :label => "Tags"
      f.input :published_date
      f.input :subcategory, :as => :select, :input_html => { :multiple => true }
    end

    f.has_many :subcatscontents do |app_f|
      app_f.inputs "Subcats Contents" do
      app_f.input :subcategories 
     end
    end

  f.buttons
 end
end

Here is the error I get in Active Admin when I go to create a new content: undefined method `klass' for nil:NilClass

Thanks for any help and I apologize in advance if something isn't semantically up to par - new to this site.

Upvotes: 2

Views: 2590

Answers (2)

LMo
LMo

Reputation: 1429

The latter answer makes them appear but not save to the database. The solution is as follows:

app/admin/contents.rb - underneath the form loop

filter :subcategories, :label => "Subcategories"

app/models/content.rb attr_accessible :subcategory_ids

has_many :subcats
has_many :subcategories, through: :subcats

app/models/subcategory.rb has_many :subcats has_many :contents, :through => :subcats

Subcats migration

class CreateSubcats < ActiveRecord::Migration
  def up
    create_table :subcats, :id => false do |t|
      t.belongs_to :subcategory
      t.belongs_to :content
      t.timestamps
    end

    add_index :subcats, [:subcategory_id, :content_id]
  end
end

Contents migration

class CreateContents < ActiveRecord::Migration
  def change
    create_table :contents do |t|
      t.string :title
      t.text :short_description
      t.text :long_description
      t.datetime :published_date
      t.datetime :edited_date
      t.string :read_length_time
      t.string :tag
      t.references :author
      t.references :contenttype
      t.string :collection
      t.integer :collection_id
      t.boolean :published

      t.timestamps
    end
  end
end

Subcategory migration

class CreateSubcategories < ActiveRecord::Migration
  def change
    create_table :subcategories do |t|
      t.string :title
      t.references :category
      t.integer :category_id

      t.timestamps
    end
  end
end

It works!

Upvotes: 2

LMo
LMo

Reputation: 1429

I added this to my ActiveAdmin.register Content do page

f.input :subcategory, :as => :check_boxes, :collection => Subcategory.all

Now all of the subcategories are showing in Active Admin page as check boxes. However, if anyone has advice as to how to make them appear as the subcategories names vs. ids, please let me know.

Thank Fivell for the lead!

Upvotes: 1

Related Questions