[英]How to display associated model's attribute in Active Admin index with belongs_to/has_many relationship (Rails 3.2/Active Admin)
I'm building a daily deal Rails app to learn RoR. 我正在建立一个每日交易Rails应用程序来学习RoR。
I am facing a problem for the past few hours : i can't get a model's attribute of an other associated model on active admin. 我在过去的几个小时内遇到了一个问题:我无法获得活动管理员上其他相关模型的模型属性。 Let me show you exactly the problem :
让我告诉你确切的问题:
I have two models: Brand (ie the brand of the deal) and Deal. 我有两种模式:品牌(即交易的品牌)和交易。 A deal belongs to a Brand but a Brand can have many Deals.
交易属于品牌,但品牌可以有很多交易。
models/deal.rb is like this: models / deal.rb是这样的:
class Deal < ActiveRecord::Base
belongs_to :brand
and we have models/brand.rb: 我们有models / brand.rb:
class Brand < ActiveRecord::Base
has_many :deals
attr_accessible :name
And i did the t.belongs_to in my migrations so this is ok. 我在我的迁移中做了t.belongs_to,所以这没关系。
In Active Admin's Deals' create form , i type, as admin, which brand the deal is associated with: 在Active Admin的交易'创建表单中,我输入与交易相关联的品牌作为管理员:
admin/game.rb 管理员/ game.rb
ActiveAdmin.register Deal do
# -- Form -----------------------------------------------------------
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :brand_id, :label => "Select a brand:", :as => :select, :collection => Brand.all
end
it works great, and i can create Deals with a certain brand. 它工作得很好,我可以创建特定品牌的优惠。 but I CAN'T manage to display the NAME of the Brand in my list of Deals :
但我无法在我的交易列表中显示品牌的名称 :
ActiveAdmin.register Deal do
index do
selectable_column
# id_column
column :title
column :deal_amount
column :brand do |deal|
link_to deal.brand.name
end
...doesn't work. ......不起作用。
How can I do that ? 我怎样才能做到这一点 ?
I tried everything but i basically don't know how to fetch the name of a Brand given it matches the brand_id in the Deal's table . 我尝试了一切,但我基本上不知道如何获取品牌的名称,因为它与Deal的表中的brand_id相匹配 。
Any help appreciated. 任何帮助赞赏。
show do |f|
panel "Subject" do
attributes_table_for f, :name, :description, :is_visible
end
panel "Pages in List View" do
table_for(f.pages) do |page|
column :name
column :permalink
column :is_visible
end
end
panel "Pages in View " do
div_for(f.pages) do |page|
panel page.name do
attributes_table_for page, :name, :description, :is_visible
end
end
end
end
end
You can do nested relations in same style as parent model 您可以使用与父模型相同的样式执行嵌套关系
A couple things seem missing: 似乎缺少一些东西:
class Deal < ActiveRecord::Base
belongs_to :brands, foreign_key: :brand_id, class_name: 'Brand'
end
This is assuming that you mean partner
to be a Brand
and your schema uses brand_id
for that relationship. 这假设您的
partner
是Brand
而您的架构使用brand_id
作为该关系。
In your form, you can simply use: 在您的表单中,您可以简单地使用:
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :partner, label: 'Select a brand:'
end
end
Your link_to
call won't actually link to a url the way you have it. 您的
link_to
调用实际上不会以您拥有的方式链接到网址。
column :brand do |deal|
link_to deal.partner.name, admin_brand_path(deal.partner)
# or simpler
auto_link deal.partner
end
I would highly recommend trying to be consistent in your naming, as it will make things a lot less confusing and will require less code to make things work. 我强烈建议尝试在命名中保持一致,因为它会使事情变得更加混乱,并且需要更少的代码才能使事情发挥作用。 ie
即
class Deal < ActiveRecord::Base
belongs_to :brand
end
f.input :brand, label: 'Select a brand:'
auto_link deal.brand
And your DB column can still be named brand_id
. 您的数据库列仍可以命名为
brand_id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.