Show child/nested attributes in ActiveAdmin index view column

点点圈 提交于 2020-01-15 09:47:26

问题


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:


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

    client.sales_agent.agent_name if !client.sales_agent.nil?


来源:https://stackoverflow.com/questions/10289138/show-child-nested-attributes-in-activeadmin-index-view-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!