我有一个具有has_and_belongs_to_many关联的ActiveAdmin项目。索引页面和表单代表我喜欢的内容。保存或编辑记录时,会正确保存关联。但是,当您保存并重定向到显示页面时,关联字段显示为空。如何自定义显示页面以显示更新的所有记录?
这是我的AA型号:
ActiveAdmin.register Products do
# MENU
menu :parent => "Items"
# FILTER
filter :name, :as => :select
# CONTROLLER
controller do
def new
@data = ItemsCategory.all
$tree = build_category_tree(@data)
@items_design = ItemsDesign.new
@items_design_show_id = nil
$hidden = "<input type=\"hidden\" name=\"items_design[items_category_ids][]\" value=\"#{params[:category_id]}\">".html_safe
end
def autocomplete_tags
@tags = ActsAsTaggableOn::Tag.
where("name LIKE ?", "#{params[:q]}%").
order(:name)
respond_to do |format|
format.json { render json: @tags, :only => [:id, :name] }
end
end
def build_category_tree(data, indent = 2)
d = data.each_with_object({}) { |h, g| g[h[:id]] = h }
options = ""
d.each { |_, h| h[:ancestor_ids] =
(h[:top_level_category_id] ? d[h[:parent_id]][:ancestor_ids] : [])+[h[:id]] }
.values
.sort_by { |h| h[:ancestor_ids] }
.each do |h|
if h[:id] == @selected
options << "<option selected value=\"#{h[:id]}\">" + " " *((h[:ancestor_ids].size-1)*indent) + "#{h[:name]}</option>"
else
options << "<option value=\"#{h[:id]}\">" + " " *((h[:ancestor_ids].size-1)*indent) + "#{h[:name]}</option>"
end
end
tree = "<select class=\"chosen\" name=\"items_design[items_category_ids][]\">" << options << "</select>"
tree.html_safe
end
def edit
@items_design_show_id = nil
@items_design_id = request.fullpath.split('/').reject! { |c| c.empty? }
@items_design = ItemsDesign.find_by_id(@items_design_id)
@selected = @items_design.items_categories[0].id
$tree = build_category_tree(@data)
end
end
# INDEX
index do
column :id
column :name
column "Description" do |desc|
truncate(desc.description, omision: "...", length: 100)
end
column "Image" do |items_design|
image_tag "#{items_design.image_name}", class: "item-image"
end
column :style
column :brand
column :color
column "Items Categories" do |category|
(category.items_categories.map { |p| p.name }).join(', ').html_safe
end
column "Colors" do |c|
(c.colors.map { |p| p.name }).join(', ').html_safe
end
column :make
column :like
column :price
column :product_url
column :tag_list
default_actions
end
form do |f|
f.inputs "Details" do
f.inputs do
$tree
end
f.input :item
f.input :name
f.input :company
f.input :description
f.input :style
f.input :brand
f.input :color
# select2 is used for multi-select + on-the-fly tag creation per the article below
# http://hoff2.com/2013/11/09/acts_as_taggable_on_active_admin_select2.html
f.input :tag_list,
label: "Tags",
input_html: {
data: {
placeholder: "Enter tags",
saved: f.object.tags.map { |t| {id: t.name, name: t.name} }.to_json,
url: autocomplete_tags_path
},
class: 'tagselect'
}
f.input :colors, :as => :select
f.input :make
f.input :like
f.input :price
f.input :product_url
end
f.actions
end
end
答案 0 :(得分:0)
这对我有用:
show do
attributes_table do
row :id
row 'Category' do |n|
n.categories.map(&:name).join("<br />").html_safe
end
row :name
row :description
end