In a Rails (erb) view, I want to conditionaly display some text and a link.
Is there anything simpler than that:
<% if some_condition %>
Go to
<%= link_to("Home", root_url) %>
<% else %>
See all
<%= link_to("objects", my_objects_path) %>
<% end %>
I am a bit annoyed by the number of <% and <%= tags.
As a helper (depending on actual usage, it could be an application-wide helper, a model helper, etc):
(I wouldn’t linkinate only a single word–smaller target, and IMO less communicative. YMMV.)
Then in the view:
As a partial it’s a matter of rendering the partial, perhaps passing the condition in as a local, or if it’s an instance variable, it can be referenced directly without needing to be passed.
(There are a few ways a partial could be handled. IMO a helper is more appropriate.)