Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

rails helpers string interpolation in erb template

I am using the ternary operator for single-line conditions, with erb printing tag <%= %>

<p>Status:
     <%= notification.read? ? "Read" : link_to "Mark as Read", "/" %>
</p>

I need to give a mark as read link in false condition, in the above scenario getting the syntax template error, here notification is an object of the Notification model.

I want output as-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

mark as read 

mark as read would be link.

thanks!

>Solution :

Don’t use a ternary:

<% if notification.read? %>
  Read
<% else %>
  <%= link_to 'Mark as Read', '/' %>
<% end %>

Or use parentheses in the link_to method call:

<%= notification.read? ? 'Read' : link_to('Mark as Read', '/') %>

Remember that anything inside <%= ... %> is just Ruby and that link_to is just a Ruby method like any other.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading