如何在Active Admin索引中显示带有归属关系到has_many关系的关联模型的属性(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)

查看:99
本文介绍了如何在Active Admin索引中显示带有归属关系到has_many关系的关联模型的属性(Rails 3.2 / Active Admin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个日常的Rails应用程序来学习RoR。

I'm building a daily deal Rails app to learn 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 (i.e the brand of the deal) and Deal. A deal belongs to a Brand but a Brand can have many Deals.

models / deal.rb就像这样:

models/deal.rb is like this:

class Deal < ActiveRecord::Base
  belongs_to :brand

,我们有模型/brand.rb:

and we have models/brand.rb:

class Brand < ActiveRecord::Base    
  has_many :deals

  attr_accessible :name

在Active Admin's Deals的create form中,我输入admin作为交易的品牌关联于:

In Active Admin's Deals' create form , i type, as admin, which brand the deal is associated with:

admin / game.rb

admin/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

...不是工作。

我该怎么做?

我尝试了所有操作,但我基本上不知道如何获取与交易表中的brand_id相匹配的品牌名称

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.

任何帮助表示赞赏。

推荐答案

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

这篇关于如何在Active Admin索引中显示带有归属关系到has_many关系的关联模型的属性(Rails 3.2 / Active Admin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆