Search code examples
ruby-on-railshyperlinkvisible

How to edit link visibility in rails based on condition?


I want a link to show depending on whether or not a user is an admin (which is a boolean attribute). So far I have this:

views/opportunities_opportunity <%= link_to_if(@user.admin?, "Delete", opportunity, method: :delete, data: {confirm: 'Are you sure?'}) %>

This will make the link active if and only if the user is an admin. Therefore, the "Delete" appears, but it is not a link to anywhere. I am afraid that it might confuse non-admins into thinking the link is broken. Does anyone know if there something along the lines of link_visible_if(@user.admin?)?

Please help! Thanks!


Solution

  • You could just use an if statement.

    <%= link_to("Delete", opportunity, method: :delete, data: {confirm: 'Are you sure?'}) if @user.admin? %>