2013-02-18 53 views
2

我想只有在當前用戶是「主人」,但即使主人的用戶ID匹配的用戶ID爲客人條件顯示Rails的

<%= link_to "X" , "/songs?name=#{s.name}&party_profile_id=#{s.party_profile_id}&commit=X", :remote => true, :class => "btn btn-inverse btn-small refreshbutton", :style => "display:none" unless @current_user.id == @party_profile.host %> 
按鈕被隱藏,顯示刪除按鈕

我使用錯誤的語法嗎?有沒有更好的方式來有條件地顯示項目?

+0

是'@ party_profile.host'一個整數,因爲'@ current_user.id'幾乎可以肯定是,我不知道,如果他們能永遠是一樣的事情。 – stephenmurdoch 2013-02-18 21:14:02

回答

5

你可以切換到只渲染它,而不是切換CSS類(除非你的應用程序需要切換到客戶端由於某種原因)。

<% if @current_user.id == @party_profile.host %> 
    <%= link_to "X" , "/songs?name=#{s.name}&party_profile_id=#{s.party_profile_id}&commit=X", :remote => true %> 
<% end %> 
1

不應該是

if @current_user.id == @party_profile.host

編輯:

您不需要將樣式設置爲無顯示。 if語句將處理它是否在視圖中呈現。

+0

不。他試圖在這種情況下將樣式設置爲「display:none」。 – jdl 2013-02-18 21:16:11

3

您的條件將適用於鏈接本身,而不適用於style屬性。

而是執行此操作:

<%= link_to "X" , "/songs?name=#{s.name}&party_profile_id=#{s.party_profile_id}&commit=X", :remote => true, :class => "btn btn-inverse btn-small refreshbutton", :style => "#{'display:none;' unless @current_user.id == @party_profile.host}" %>