1

I can't figure out the syntax to embed html inside my erb pe field. This is what I'm trying to do ( and it works when I write it this way.

<% if weekday == "Monday" %>
  <b> <%= 'Mondays' %> </b>
<% else %>
  <%= 'Mondays' %>
<% end %> 

But I'm trying to get this to work but can't figure out the syntax. I'm getting a syntax error due to the html

<%= weekday == "Monday"? <b>'Mondays'</b> : 'Mondays' %>

Any idea? THank you

Louis Aude
  • 21
  • 1

3 Answers3

1

One solution would be to use content_tag; that would be explicit about the mixing of code and content, and leverages a Rails convention.

<% weekday = 'Monday' %>
<%= weekday == 'Monday' ? content_tag(:b, 'Mondays') : 'Mondays' %>

Result: Mondays

FWIW, I think your first code sample is the way to go. An answer to a similar question makes this point well.

Jake Worth
  • 5,490
  • 1
  • 25
  • 35
  • 1
    Thank you. Yeah it became quickly apparent as I started adding code into my if else statement. Nonetheless, I'm happy to know how I would do that if I were to want to. – Louis Aude Apr 03 '21 at 12:44
0

You have to specify that the string is HTML safe...

<%= weekday == "Monday" ? "<b>'Mondays'</b>".html_safe : "'Mondays'" %>
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
0

I would use the tag helper:

<%= weekday == 'Monday' ? tag.b('Mondays') : 'Mondays' %>

Btw I do not know where the weekday variable is coming from and how you initialized it. But if you created it from an instance of Date or Time then you might be interested in the monday? method too.

spickermann
  • 100,941
  • 9
  • 101
  • 131