0
<% @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?

1

3 Answers 3

2

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>" %>
Sign up to request clarification or add additional context in comments.

3 Comments

That still gives me the same issue. It shows the entire statement with the html tags included. It escapes the HTML.
@DaveyGravy, Please try the updated answer. I think you want to use the html_safe method which should give you your desired output.
html_safe should not be used in ordinary views, use <%= raw "..." %> or <%== "..." %> instead.
0

You can also style your list items this way:

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

Just based upon preference.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.