Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <%== %> do in rails erb?

I saw this recently, thought it was interesting. But I don't really understand what it does?

Ex. I have a rails app and I want to bootstrap some json, so that I don't have to make a second request. Normally I would write something like this.

<%= raw @model.to_json %> or <%= @model.to_json.html_safe %>

I have to send the message raw or html_safe or the json will be html escaped and thus not parsed correctly. However, this seems to work too.

<%== @model.to_json %>

But I can't find any documentation.

Does anyone know what this does exactly? i.e. Is it the exact same as calling html_safe or raw? Or is there more to it?

like image 948
mwoods79 Avatar asked Oct 30 '12 15:10

mwoods79


People also ask

What does ERB do in Ruby?

ERB (or Ruby code generated by ERB) returns a string in the same character encoding as the input string. When the input string has a magic comment, however, it returns a string in the encoding specified by the magic comment.

What does ERB stand for in File_name ERB?

erb stands for "Embedded RuBy".

What is ERB in Ruby on Rails?

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. ERB is Rails default engine for rendering views. Note: Rails uses an implementation called erubi instead of the ERB class from the Ruby standard library.

What are .ERB files?

Script written in ERB, a templating language for Ruby; may include any type of plain text or source code, but also includes Ruby ERB code that generates additional text into the resulting file when run with the ERB template engine. ERB is often used for templating Web files such as . RB, . RHTML, .


2 Answers

<%== is equivalent to raw.

From the Ruby on Rails Guide:

To insert something verbatim use the raw helper rather than calling html_safe:

<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %> 

or, equivalently, use <%==:

<%== @cms.current_template %> <%# inserts @cms.current_template as is %> 
like image 131
Stefan Avatar answered Oct 09 '22 20:10

Stefan


Rails actually uses Erubis instead of ERB, which supports a variety of other stuff.

<%== is exactly as you expect, though: It emits the value unescaped

like image 31
Nevir Avatar answered Oct 09 '22 21:10

Nevir