只有当当前用户是"主机"时,我才会尝试显示删除按钮,但即使主机的用户id与访客的用户id匹配,该按钮也会隐藏
<%= 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 %>
我用错语法了吗?有没有更好的方法来有条件地显示项目?
您可以切换到只渲染它,而不是切换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 %>
您的条件将应用于链接本身,而不是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}" %>
它不应该是吗
if @current_user.id == @party_profile.host
?
编辑:
您不需要将样式设置为不显示。if语句将处理是否在视图中呈现它。