2017-10-13 63 views
1

我需要在一些頁面上顯示使用button_to創建的兩個按鈕(例如:list/id1/edit)並隱藏所有其他頁面上的一個按鈕(例如:list/new)。我有這個:隱藏按鈕,如果你在某些頁面

= button_link t("list.save"), list_path(@list.save_id), class: "button"  
= button_link t("list.cancel"), list_path(@list.hashed_id), class: "button" 

如何創建「如果你在頁面X顯示一個按鈕,否則 - 2按鈕」?

回答

2

我喜歡@阿瓊的答案(尤其是@ RyanWilcox的評論),雖然只是扔東西進入的戒指,你「也就會擁有訪問controller_nameaction_name幫手......

unless controller_name == 'list' && action_name == 'new' 
    show_the_button 
end 

- unless controller_name == 'list' && action_name == 'new' 
    = button_link t("list.save"), list_path(@list.save_id), class: "button"  
    = button_link t("list.cancel"), list_path(@list.hashed_id), class: "button" 

這可以是在限制/允許某些控制器和/或動作的方面非常靈活。

+0

'current_page?'helper也提供這個。 Rails很酷! – arjun

相關問題