Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render <%= %> as a String in an .html.erb View?

I want to print following as a string in my .html.erb view.

<%= selected_color %>

So, I tried following in my file:

<h3><%= '<%= selected_color %>' %></h3>

It gave me following error:

syntax error, unexpected $undefined, expecting ')'

If I remove <%= '<%= selected_color %>' %> and place some string like abc, then it is printed on page without any issue.

As it is static page, I don't want to create a controller for it. So, can you help in showing that string on page?

p.s. I'm using ruby v1.9.3 and rails v3.1.0

like image 408
Abhishek Avatar asked Aug 07 '15 13:08

Abhishek


People also ask

How do you render a partial?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is HTML ERB file?

ERB is a templating engine. A templating engine allows you to mix HTML & Ruby so you can generate web pages using data from your database.

How do I comment in an ERB file?

erb is by definition "embedded ruby", you can embed every ruby code between: <%= and the other: %> , typically all written in one line. In addition, ruby one-line comments start always with # , so the <%=# Comment %> style matches perfectly with both pure-ruby and erb styles for one-line comments.


2 Answers

You should double the % symbols as follow:

<h3><%%= rating_color %></h3>

Edit for source:

In erb.rb line 50, we see that <%% is a special tag that is replaced by <% we can also see that on line 650.

like image 99
ex0ns Avatar answered Nov 04 '22 17:11

ex0ns


Pure HTML

&lt;%= selected_color %&gt;

or double % as mentioned by @ex0ns

<%%= selected_color %>

like image 24
kjmagic13 Avatar answered Nov 04 '22 16:11

kjmagic13