活动管理员中嵌套资源下的未定义方法

时间:2014-10-13 02:42:01

标签: ruby-on-rails activeadmin

我有2个模型Tool::Cvt::RemoteFocuTool::Cvt::RemoteFocuLog

Tool::Cvt::RemoteFocu has_many Tool::Cvt::RemoteFocuLog

并且每个Tool::Cvt::RemoteFocuLog都有一个snapshot用于显示图片(由回形针处理)

我想在Tool::Cvt::RemoteFocu中显示快照,但我得到了未定义的方法错误。

undefined method `snapshot' for nil:NilClass

但实际上Tool::Cvt::RemoteFocuLog中的每条记录都有snapshot方法。

ActiveAdmin.register Tool::Cvt::RemoteFocu do

   f.inputs "Remote focus images" do
     f.has_many :remote_focu_logs do |p|
       p.input :snapshot, :as => :file, :label => "Image",:hint => p.object.snapshot.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.snapshot.url(:small))
       p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
     end
   end

ActiveAdmin.register Tool::Cvt::RemoteFocuLog do

~~~~

1 个答案:

答案 0 :(得分:-1)

尝试使用:hint => p.object.try(:snapshot).nil?代替:hint => p.object.snapshot.nil?