在我的Ruby on Rails应用程序中,我的方法如下:
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]
为什么这段代码不起作用?
答案 0 :(得分:2)
尝试明确设置id
:
admin_survey_pack_sign_off_path(id: sp.survey_pack_sign_off)
如果我正确理解了您的代码,survey_pack_sign_off
包含该页面的一些ID
答案 1 :(得分:0)
解决方案是:
spso = sp.survey_pack_sign_off
link_to_if spso, signed_off, spso ? admin_survey_pack_sign_off_path(spso) : nil