无法在关联模型的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

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

问题描述

我正在构建一个日常的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 / deal.rb

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

效果很好,而且我可以创建具有特定品牌的交易。但我无法在Active Admin'x索引的交易列表中显示品牌名称:

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 iun Active Admin'x index :

ActiveAdmin.register Deal do
index do   
  selectable_column   
  # id_column 
  column :title
  column :deal_amount
  column :brand do |deal|
    link_to deal.brand.name, admin_brand_path(deal.brand)
  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.

任何帮助表示赞赏。

更新
我遇到的错误是它不理解函数.name:未知方法'name'

UPDATE the error i'm getting is it doesn't understand function .name: unknown method 'name'

推荐答案

您需要处理 deal.brand nil

ActiveAdmin.register Deal do
  index do   
    selectable_column   
    # id_column 
    column :title
    column :deal_amount
    column :brand do |deal|
      if deal.brand.present?
        link_to deal.brand.name, admin_brand_path(deal.brand)
      else
        status_tag('Empty')
      end
    end
  end
end

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

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