使用 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 中遇到的错误: undefined method `klass' for nil:NilClass
感谢您的帮助,如果某些内容在语义上不符合标准,我提前道歉 - 本网站的新内容。