Show child/nested attributes in ActiveAdmin index

2019-08-07 11:37发布

I'm trying to show the value of a nested attribute in ActiveAdmin.

My code is as follows:

index do
  column :code
  column 'Sales Agent' do |client|
    client.sales_agent.agent_name
  end
end

This gives me:

NoMethodError in Admin/client_branches#index

Showing /Users/constantlm/.rvm/gems/ruby-1.9.2-p290/gems/activeadmin-0.4.3/app/views/active_admin/resource/index.html.arb where line #1 raised:

undefined method `agent_name' for nil:NilClass

I don't understand why this doesn't work, because when I do the following:

index do
  column :code
  column 'Sales Agent' do |client|
    raise client.sales_agent.agent_name.inspect
  end
end

It outputs:

RuntimeError in Admin/client_branches#index

Showing /Users/constantlm/.rvm/gems/ruby-1.9.2-p290/gems/activeadmin-0.4.3/app/views/active_admin/resource/index.html.arb where line #1 raised:

"Peter John"

Which is what I would expect the first statement to do as well (referring to the "Peter John"), but it's not. What am I missing?

1条回答
Animai°情兽
2楼-- · 2019-08-07 12:20

Clear your records and start with new records. Additionally try adding

    client.sales_agent.agent_name if !client.sales_agent.nil?
查看更多
登录 后发表回答