55

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?

mwoods79
  • 1,528
  • 12
  • 15

2 Answers2

64

<%== 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 %>
Stefan
  • 96,300
  • 10
  • 122
  • 186
  • Looked at the rails guides, don't know how I missed that? Thanks! – mwoods79 Oct 30 '12 at 15:55
  • can this be used in interpolation? `"#{@something}

    Is here

    "` or would you just use html_safe at the end of the string?
    – Jordan Jul 08 '15 at 20:43
  • 1
    Thanks for the link, it's really hard to google for this since google seems to strip those characters out and oddly there's many guides and wikipedia articles that have no mention of `` – CTS_AE Feb 12 '19 at 20:19
11

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

Nevir
  • 7,333
  • 3
  • 36
  • 49
  • 1
    https://github.com/rails/rails/blob/master/actionpack/lib/action_view/template/handlers/erb.rb#L59 – Nevir Jan 18 '13 at 08:23
  • If you add something to address my original question, "Is it the exact same as calling html_safe or raw? Or is there more to it?". I will accept this as an answer. When I first read this I thought "awesome", and almost accepted it then. – mwoods79 Jan 18 '13 at 20:35
  • I guess its not exactly the same, as its code in Erubis, versus the html_safe/raw methods which are implemented elsewhere (rails?). Its goal is to make it easier to embed stuff 'as is'. I agree its not documented well - was going to post the same question myself when I found this one. PS The accepted answer is also correct too :) – Chris Kimpton Jan 19 '13 at 08:46