简体   繁体   English

如何使用belongs_to / has_many关系在Active Admin索引中显示关联模型的属性(Rails 3.2 / Active Admin)

[英]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. 这假设您的partnerBrand而您的架构使用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.

相关问题 无法在关联模型的Active Admin索引属性中显示(longs_to / has_many)-Rails 3.2 - Can't display in Active Admin index attribute of associated model (belongs_to/has_many) - Rails 3.2 迁移时是否需要对belongs_to / has_many关系使用add_index? (Rails 3.2,Active Record) - Need to use add_index on migration for belongs_to/has_many relationship? (Rails 3.2, Active Record) rails活动管理界面:has_many-从包含has_many的模型中进行选择,而不是从包含belongs_to的frm中进行选择 - rails active admin interface : has_many - select from the model containing has_many, not frm that containing belongs_to 堆栈级别太深,在Rails API和active_model_serializer中属于belongs_to / has_many关系 - Stack level too deep, belongs_to/has_many relationship in Rails API and active_model_serializer 通过关系使用has_many上的关联值来过滤活动管理员中的表(Rails 4 /活动管理员) - Use associated values on has_many through relations to filter a table in active admin (Rails 4/Active Admin) 活动记录迁移 rails 4 中的 has_many、belongs_to 关系 - has_many, belongs_to relation in active record migration rails 4 Rails模型关系has_many当属 - Rails Model relationship has_many belongs_to 无法访问has_many当属关系关系模型Rails 5 - Unable to access model for has_many belongs_to relationship Rails 5 向Active Admin添加项目归属关系 - adding items belongs_to relationship to Active Admin 如何在rails中跨越belongs_to和has_many关系? - How to span belongs_to and has_many relationship in rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM