2015-04-02 88 views
1

在我的Ruby on Rails应用程序我有方法,看起来像这样:问题和Rails的link_to如果方法

def survey_pack_signed_off(sp, type) 
    signed_off = Util::Boolean.humanize(sp.survey_pack_sign_off.present?)#returns Yes if true and No if false 
    return signed_off if type == :csv 
    if sp.survey_pack_sign_off.present? 
     link_to signed_off, admin_survey_pack_sign_off_path(sp.survey_pack_sign_off) 
    else 
    signed_off 
    end 
end 

我试图重构它和使用Rails的link_to_if方法:

def survey_pack_signed_off(sp, type) 
    signed_off = Util::Boolean.humanize(sp.survey_pack_sign_off.present?)#returns Yes if true and No if false 
    return signed_off if type == :csv 
    link_to_if (type == :html && sp.survey_pack_sign_off.present?), signed_off, admin_survey_pack_sign_off_path(sp.survey_pack_sign_off) 
end 

但此link_to_if导致以下错误:

ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"admin/survey_pack_sign_offs", :format=>nil, :id=>nil} missing required keys: [:id] 

为什么此代码无效?

+1

你能向我们展示行动'survey_pack_sign_offs'的路线? – RPinel 2015-04-02 16:00:59

回答

0

解决的办法是:

spso = sp.survey_pack_sign_off 
    link_to_if spso, signed_off, spso ? admin_survey_pack_sign_off_path(spso) : nil 
2

尝试设置id明确:

admin_survey_pack_sign_off_path(id: sp.survey_pack_sign_off) 

如果我理解正确的代码,并survey_pack_sign_off包含页面的一些ID

+0

不幸的是,这不工作... – 2015-04-03 08:15:19