關於rails 4.0中link_to_if的問題,因爲我剛升級。舊的Rails 3版本沒有問題。link_to_if in Rails 4.0
簡單的例子:
link_to_if(user, "User", user.id)
content_tag(:span, "No User")
end
因此,對於一個給定的用戶對象,它只是設置其ID爲鏈接。我現在的問題是如果沒有用戶(無)/甚至可以在這裏硬編碼「錯誤」。在舊的Rails 3中,它忽略了這些選項。
在Rails 4中,它奇怪地觸發了選項,即使條件沒有滿足。爲什麼?我是否真的需要檢查零,即使條件已經做到了?
編輯,真正的代碼:
def link_to_previous_poster
link_to_if(@poster.prev, "< Previous".html_safe, poster_path(@poster.prev, :skip => skip_poster.to_i-1), :accesskey => 'h') do |name|
content_tag(:span, '< Previous'.html_safe, :class => "current")
end
end
在@ poster.prev ==零觸發它與導致路由錯誤的零值poster_path的情況。
瀏覽器的錯誤是:
的ActionController :: UrlGenerationError在海報#顯示 顯示/.../show.html.haml其中第5行提出:
沒有路由匹配{:動作=> 「節目」,:控制器=> 「海報」,:ID =>零,:格式=>零,:跳過=> - 1}缺少必需的鍵:[:ID]
您是否嘗試過'link_to_if(user.present? 「用戶」,user.id)等]'? – MrYoshiji
向我們顯示錯誤。 – Serabe
它不起作用,即使我使用'link_to_if(false,「User」,user.id)'它調用user.id – Calmon