Active Admin has_many通过表格不在表单中呈现 - 已尝试过所有内容

时间:2013-09-06 22:30:44

标签: ruby-on-rails activeadmin has-many-through

使用Rails 3.2并在Active Admin的has_many:through中遇到很多麻烦。目标是通过内容模型引入has_many:through子类别,并为所有子类别创建复选框。我已经阅读了Active Admin文档,搜索了堆栈,github等上的其他文章。没有什么工作。我的唯一猜测可能是“form:html => {:enctype =>”multipart / form-data“} do | f |”标签,我需要PaperClip。

*我尝试过使用accepts_nested_attributes但它不起作用......

对于子类别和内容,有一个has_many:through表。 以下是数据库中的样子:

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"

以下是模型:

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

然后在我的主动管理表单中,我尝试了所有内容......这是当前状态:

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

以下是我在创建新内容时在Active Admin中遇到的错误: 未定义的方法`klass'代表nil:NilClass

感谢您提供任何帮助,如果某些内容不符合本网站的新标准,我会提前道歉。

2 个答案:

答案 0 :(得分:2)

后一个答案使它们出现但不保存到数据库中。解决方案如下:

app / admin / contents.rb - 在表单循环下面

filter :subcategories, :label => "Subcategories"

应用程序/模型/ content.rb     attr_accessible:subcategory_ids

has_many :subcats
has_many :subcategories, through: :subcats

应用程序/模型/ subcategory.rb     has_many:子类     has_many:contents,:through =&gt; :subcats

Subcats迁移

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

内容迁移

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

子类别迁移

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

有效!

答案 1 :(得分:1)

我将此添加到我的ActiveAdmin.register内容页面

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

现在所有子类别都在Active Admin页面中显示为复选框。但是,如果有人建议如何使它们显示为子类别名称与ids,请告诉我。

感谢Fivell的领导!