Use Ruby Array for a JavaScript Array in Erb. Escaping Quotes

Use ruby array for a javascript array in erb. Escaping quotes

to_json is working fine. What you're running into is Rails 3.x's XSS protection. There's a good article on this at Railscasts/ASCIIcasts. The gist, though, is that you need to use the raw or html_safe methods:

In your controller:

@t_json = @t.to_json.html_safe

OR in your view:

<%= raw @t %>

how to render Ruby array as JavaScript array in ERB template

Why don't your just use to_json?

# in ruby
@array = [{:text=>"Lorem", :weight=>15}, {:text=>"Ipsum", :weight=>15}]

Then, in the view:

var wordArray = <%= @array.to_json.html_safe %>;

Which will render:

var wordArray = [{"text":"Lorem","weight":15},{"text":"Ipsum","weight":15}];

Escaping in Rails and Javascript

Use <%= raw your_variable %> :)

Escaping quotes from Rails Variables when using them for Javascript?

when you try alert('I\'m testing'); there's a problem

Backslash is also an escape in Ruby strings! So the string literal:

"alert('I\'m testing');"

means the string:

alert('I'm testing');

the backslash is gone already before JavaScript gets a look at it. When you are writing a JavaScript string literal inside a Ruby string literal you need to escape the escape, \\, to get a real \ that will then, in JavaScript, escape the apostrophe.

escape_javascript correctly generates the backslash for JavaScript, if a backslash was included in its input. But again, if you're writing a string literal, you have to escape the backslash to get a real backslash:

escape_javascript("\b")     -> this is a backspace character!
escape_javascript("\\b") -> this is backslash-then-letter-b;
escaped for JavaScript literal to double-backslash-then-b.

So, this is fine:

"'"+escape_javascript(myvar)+"'"

alternatively, you can use a JSON encoder to create the JavaScript string literal including the surrounding quotes.

Ruby array to Javascript array

Let's assume you are using erb. A first approach:

<%= javascript_tag "account_ids = #{account_ids.to_json.html_safe};" %>

The problem is that this creates a global variable without context (who uses it?). That's why I'd rather call a function defined somewhere in your JS code:

<%= javascript_tag "setAccounts(#{account_ids.to_json.html_safe});" %>

How to use a controller's array in js.erb without converting array to a string?

Use this:

@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'].to_json.html_safe

And use it like this, with no surrounding " and you don't need to escape it (it's already scaped with to_json and html_safe):

console.log(<%= @labels %>);
labels: <%= @labels %>,

Keeping a Sinatra app light: Displaying a raw array without loading ActionView

I'm not sure why you get some escaping on the array right now, but this is what I put in an ERB view:

<% array = ["Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue"] %>

<p><%= array.inspect %></p>
<p><%= array.inspect.gsub(/"/, '\"') %></p>

and this was the output:

["Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue"]

[\"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\", \"Mon\",
\"Tue\"]

But the real question is "How do I get data from Sinatra into a javascript function?"

class App < Sinatra::Base

get "/array", :provides => :json do
content_type :json
["Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue"].to_json
end

end

and then call it via some jQuery (or whichever library you would prefer) compiled from coffeescript, of course :)

$.getJSON "/array", (res) ->
# do something with the result

Which means:

  • you don't need to worry about escaping
  • the jQuery takes care of parsing the JSON response so you've just to work with an object
  • it's easier to test
  • you've got an API set up with a web app calling against it (i.e. the Sinatra way not the Rails way)
  • and you'll be doing something more similar to other people which will make getting help easier.


Related Topics



Leave a reply



Submit