2015-04-02 86 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