2

I'm trying to display a delete button only if the current user is the "host" but the button is hidden even if the user id for the host matches the user id for the guest

<%= 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 %>

Am I using the wrong syntax? Is there a better way to conditionally display items?

1
  • Is @party_profile.host an integer, because @current_user.id almost certainly is, and I wonder if they can ever be the same thing. Commented Feb 18, 2013 at 21:14

3 Answers 3

5

You could switch to only render it, rather than toggling the css class (unless your application would need to toggle it client side for some reason).

<% 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 %>
Sign up to request clarification or add additional context in comments.

Comments

3

Your condition would apply to the link itself, not the style attribute.

Do this instead:

<%= 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}"  %>

Comments

1

shouldn't it be

if @current_user.id == @party_profile.host ?

Edit:

you don't need to set the style to display none. The if statement will handle whether it is rendered in the view.

1 Comment

No. He's trying to set the style to 'display:none' in those cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.