问题
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