Conditional link_to
In your Rails view, you can conditionally render links thanks to ActionView::Helper using #link_to_if
<%= link_to_if(current_user.nil?, "Login", login_path) %>
When this condition is true it would output:
<a href="/sessions/new/">Login</a>
When it is false it just outputs the text:
Login
If you want a custom rendering for false values, just pass that into a block:
<%= link_to_if(current_user.nil?, "Login", login_path) do
link_to("Logout", logout_path)
end %>
NOTE: There is also an inverse
link_to_unlessmethod available
Here's the docs if you're interested.
Tweet