0

I need to get a ruby array in to a javascript array and I'm getting a parse error.

var characters = <%= @poop.to_json %>;

That is how I'm embedding ruby into inline javascript and it's coming up with a parse error. How should I be getting this ruby array into javascript?

I'm embedding this into an .html.erb file so ruby should be getting to the variable before javascript.

This is what safari console is showing:

alt text

Update

I had a form that had three fields js, css, and html respectively so I would put my javascript in a form and then it would put it into the header.

<head>
  <%= @game.javascript %>
 </head>

So as you can see I was embedding ruby in javascript that was the output of a ruby object so the reason ruby wasn't getting to it first was that it had already got to it and treated the <%= %> just like another string.

Ahh, I up voted you all for the help. Thanks!

4
  • Put it in a separate Ruby script and send the appropriate content type header. Commented Dec 19, 2010 at 23:57
  • make this an answer and explain more please :) Commented Dec 20, 2010 at 0:06
  • Can you show us a longer code sample? I don't understand at the moment why <%= isn't being parsed if this is an erb file. Commented Dec 20, 2010 at 0:11
  • I just think I got it. fme! some custom thing I had so you guys can hold off for a second and I will post what happened. Thanks everyone for the help. Commented Dec 20, 2010 at 0:14

3 Answers 3

4

You're treating a .js file like an .erb file - it's not. JavaScript files aren't parsed by Ruby - anything in public is sent as-is.

One way to get @poop into your JavaScript is to have a Rails action that renders json, then load that with XHR.

Alternatively, you could output @poop in the HTML template in a <script> tag, then load the contents of that script tag from your JavaScript.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm assuming it's a js.erb file or inline javascript.
I'm embedding it into .html.erb
@Sam - That's strange - I don't understand how the text <%= %> is getting to Safari without being parsed.
2

Ideally, we need more information to properly answer, but I'm going to guess some stuff to help you...

Assumption #1: You are using erb syntax (those "<%=" and "%>") in a .js file. These aren't parsed by Rails into anything meaningful.

What you want to do, is use inline JS in your template or view. Javascript generation from other languages can be tricky though, use with caution.

Comments

0
var characters = "<%= @poop.to_json -%>";

3 Comments

I'm looking at it working here but maybe that's haml. Try with "<%= -%>". @skilldrick, js.erb works fine as does inline javascript. As for downvoter, comment! There's no mention of best practice in the question.
js.erb is fine, but you still have to say when you're using Ruby - at the moment this is just JavaScript, and you're assigning a string that starts "#{" - the Ruby interpreter will only parse it if it's in <%= %>.
Yeah sorry I use haml which I guess accepts my former suggestion where erb does not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.