views:

14

answers:

1

Why does link_to_if show the link title if the condition is false?

I'm using it on a DELETE, and if the condition is false, Rails shows delete but it isn't linked. I don't want DELETE to show up if it's false.

Is there a setting or another helper for that?

Thank you

+3  A: 

If you just want the link to disappear completely, just use an 'if' around the link_to, rather than link_to_if.

<% if condition %>
  <%= link_to ... %>
<% end %>

If you want something else to appear when the condition is not true, you can pass a block to render instead. Having said that, from a stylistic perspective, I think I prefer 'if' 'else' to the block syntax - it's less confusing, at least to my eye.

Block syntax:

<%= link_to_if(condition, "Link Title", dest ) { link_to( "Alt Title", alt_dest ) } %>

If/else syntax:

<% if condition %>
  <%= link_to "Link Title", dest %>
<% else %>
  <%= link_to "Alt Title", alt_dest %>
<% end >

For more information see the documentation for UrlHelper.

Paul Russell