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

Ruby on-RailsRubyRuby on-Rails-3Erb

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

<%== 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 %>

Solution 2 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmwoods79View Question on Stackoverflow
Solution 1 - Ruby on-RailsStefanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsNevirView Answer on Stackoverflow