在ActiveAdmin索引视图列中显示子/嵌套属性
我试图在ActiveAdmin中显示嵌套属性的值。
我的代码如下:
index do column :code column 'Sales Agent' do |client| client.sales_agent.agent_name end end
这给了我:
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
我不明白为什么这不起作用,因为当我做以下事情时:
index do column :code column 'Sales Agent' do |client| raise client.sales_agent.agent_name.inspect end end
它输出:
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"
这也是我所期望的第一个声明(指“彼得约翰”),但事实并非如此。 我错过了什么?
清除记录并从新记录开始。 另外尝试添加
client.sales_agent.agent_name if !client.sales_agent.nil?