I have a partial which is used all over the site. I have an image in the partial that I want to link to a certain page on all pages except one particular controller action.
1. Is there way to put in the following href without 2 if else sets?…
<a href="#">
<img src="#" />
</a>
I’m trying to avoid doing this:
<% if (current controller != xyz) %>
<a href="#">
<% end %>
<img src="#" />
<% if (current controller != xyz) %>
</a>
<% end %>
2. How can I actually check which controller called this partial?
The above code is only pseudo code.
PS. I know this isn’t that much repetition, but I’m just using this simplified case as an example. Curious if there’s something in Rails I don’t know about to avoid this.
This is exactly what
link_to_ifis for!This will add the image whether the condition is true or not, and wrap it in the link only when the condition is true. There’s also the related
link_to_unlesswhich may be more semantically appropriate in your case.Edit: I’ve updated the example above to use the correct code for checking the controller name. There’s a method available in the view,
controller_namethat returns a lowercase version of the name of your controller. It’s also in theparamshash.