【问题标题】:Active Admin has_many through table not rendering in form - have tried everything活动管理员 has_many 通过表格未呈现形式 - 已尝试一切
【发布时间】:2013-09-06 22:30:43
【问题描述】:

使用 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 中遇到的错误: nil:NilClass 的未定义方法“klass”

感谢您的帮助,如果某些内容在语义上不符合标准,我提前道歉 - 本网站的新内容。

【问题讨论】:

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


【解决方案1】:

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

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

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

子猫迁移

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

有效!

【讨论】:

    【解决方案2】:

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

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

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

    感谢 Fivell 的领导!

    【讨论】:

    • 我也有同样的问题。你想清楚了吗?
    • 您还需要这方面的帮助吗?
    • 您可能需要将子类别添加到允许的参数中...只需查看服务器日志,它会告诉您是否允许::subcategory_ids => []
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多