我正在使用ActiveAdmin构建管理界面。所以我有文章和产品资源,在文章中显示我想制作一个侧边栏,看看分配给这篇文章的产品。补充工具栏看起来很好,但它不会显示任何记录。
Rails 4.1.0
ActiveAdmin 1.0.0
ruby 2.1
应用/管理/ article.rb
ActiveAdmin.register Article, { :sort_order => :name_asc } do
# Permitted parameters
permit_params :title, :description
# Displayed columns
index do
selectable_column
column :title
column :description
default_actions
end
# Sidebar for Products by this Article
sidebar 'Products by this Article', :only => :show do
table_for Product.joins(:article).where(:article_id => article.id) do |s|
s.column("Title") { |product| product.title }
end
end
# Filters for title and description
filter :title, :as => :select # :check_boxes (for checkboxes)
filter :description, :as => :select
end
应用/管理/ product.rb
ActiveAdmin.register Product, { :sort_order => :name_asc } do
# Permitted parameters
permit_params :article_id, :title, :description, :price
# Displayed columns
index do
selectable_column
column :article, :sortable => :article
column :title
column :description
# Currency helper
column :price, :sortable => :price do |cur|
number_to_currency cur.price, locale: :ru
end
default_actions
end
# Filters for each column within "description"
filter :article, :as => :select # :check_boxes (for checkboxes)
filter :title, :as => :select
filter :price
end
应用/模型/ article.rb
class Article < ActiveRecord::Base
# Relationship
has_many :products
# Validations
validates :title, :description, :presence => true
end
应用/管理/模型/ product.rb
class Product < ActiveRecord::Base
# Relationship
belongs_to :article
# Validations
validates :article, :title, :description, :price, :presence => true
end