DaveyGravy
DaveyGravy

Reputation: 21

Writing HTML code with Rails

<% @ticket.conversations.each do |c| %>

<section class="messages">

<%="<li> #{c.the_message} </li>" %>

</section>

<%end%>

I am trying to have rails write the HTML code for me so the output would look something like this:

<li>MESSAGE1</li>
<li>MESSAGE2</li>
<li>Next message here...</li>

I am going to style every nth element to have a different style to show what speaker it belongs to. But currently is just outputs straight text and escapes the HTML. How do I stop this escape?

Upvotes: 0

Views: 1512

Answers (3)

vee
vee

Reputation: 38645

To output you need to use <%= as follows within your <section> block:

<%= "<li> #{c.the_message} </li>".html_safe %>

But currently is just outputs straight text and escapes the HTML

You can use the html_safe method. Please refer to the "Extensions to String" topic in this document: http://guides.rubyonrails.org/active_support_core_extensions.html

Another option you can use is the raw helper(as pointed out by Stefan) which calls the html_safe for you. e.g.

<%= raw "<li> #{c.the_message} </li>" %>

Upvotes: 2

Stefan
Stefan

Reputation: 114138

Try it this way:

<% @ticket.conversations.each do |c| %>
  <section class="messages">
    <li><%= c.the_message %></li>
  </section>
<% end %>

Or if you don't want to repeat <section> every time:

<section class="messages">
  <% @ticket.conversations.each do |c| %>
    <li><%= c.the_message %></li>
  <% end %>
</section>

Upvotes: 0

lurker
lurker

Reputation: 58224

You can also style your list items this way:

<li><%= c.the_message %></li>

Just based upon preference.

Upvotes: 0

Related Questions