dcalixto
dcalixto

Reputation: 411

Rails embed html tag inside an erb tag?

I'm trying to do this:

<%= <h1>Products Purchased </h1> if  params[:status].nil? || params[:status] == Order.statuses[0]  %>

<%= "<h1>Products Sent </h1>" if  params[:status].nil? || params[:status] == Order.statuses[1]  %>

Thanks for any help.

Upvotes: 1

Views: 12689

Answers (2)

Fumisky Wells
Fumisky Wells

Reputation: 1209

Here alternative is:

<h1>
  <%= 'Products Purchased' if cond1 %>
  <%= 'Products Sent'      if cond2 %>
</h1>

Or, you can use content_tag helper method for any HTML tag:

<%= content_tag(:h1, 'Products Purchased') if cond1 %>
<%= content_tag(:h1, 'Products Sent') if cond2 %>

Where, cond1 and cond2 are "params[:status].nil? || ..." as you specify

(I wonder what happens when both cond1 and cond2 are false, but I think it is out of this topic)

Upvotes: 1

MrYoshiji
MrYoshiji

Reputation: 54902

You need to use .html_safe to output HTML tags from a ruby string:

<%= "<h1>Products Sent </h1>".html_safe if params[:status].nil? || params[:status] == Order.statuses[1]  %>

But you can do the following, more readable:

<% if params[:status].nil? || params[:status] == Order.statuses[1] %>
  <h1>Products Sent</h1>
<% end %>

Upvotes: 11

Related Questions