Dau*_*ell 1 ruby many-to-many ruby-on-rails activeadmin
在我的应用程序中,我有categories,labels 他们都属于products
在管理面板中,用户可以添加产品,创建标签和类别,然后为产品分配类别和标签.
当我创建标签时,名称在标签面板中正确显示(见照片)
问题是标签名称在分配给产品时没有正确显示(见下图),但显示了类别名称.这很奇怪,因为两列都具有相同的设置.
在Admin products panel标签上显示数字Label#1``Label#2等(见下图)
在_navbar.html.erb我有这个代码显示类别和标签...并在那里正确显示.
<ul class="dropdown-menu" role="menu">
<% @categories.each do |category| %>
<li><%= link_to category.name, category %></li>
<% end %>
</ul>
</li>
<li class="dropdown full-width">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown">
Labels
</a>
<ul class="dropdown-menu" role="menu">
<% @labels.each do |label| %>
<li><%= link_to label.label_name, label %></li>
<% end %>
</ul>
</li>
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?我看不出为什么标签显示不正确.
有没有人与活动管理员有相同或类似的问题?
谁能看看这个?
提前致谢
这是 app/admin/category.rb
ActiveAdmin.register Category do
permit_params :name
end
Run Code Online (Sandbox Code Playgroud)
在app/admin/label.rb这里有代码:
ActiveAdmin.register Label do
permit_params :label_name
end
Run Code Online (Sandbox Code Playgroud)
并在 app/admin/product.rb
ActiveAdmin.register Product do
permit_params :title, :description, :image, :price_usd, :price_isl, :category_id, :label_id
index do
column :title
column :category
column :label
column :created_at
column :price_isl, :sortable => :price_isl do |product|
number_to_currency product.price_isl
end
column :price_usd, :sortable => :price_usd do |product|
number_to_currency product.price_usd
end
actions
end
end
Run Code Online (Sandbox Code Playgroud)
然后是Schema.rb的一部分
create_table "labels", force: :cascade do |t|
t.string "label_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Run Code Online (Sandbox Code Playgroud)
Active Admin在显示对象时进行有根据的猜测.
这是它尝试按顺序调用的方法列表
因此,它适用于类别,因为类别具有:name属性.
对于labels您可以执行以下操作:
class Label < ActiveRecord::Base
def display_name
label_name
end
end
Run Code Online (Sandbox Code Playgroud)
您还可以在ActiveAdmin初始化程序中配置显示的名称:
ActiveAdmin.setup do |config|
config.display_name_methods = [:display_name, :full_name, :name, ..., :to_s]
end
Run Code Online (Sandbox Code Playgroud)