#324 Passing Data to JavaScript
There are a variety of ways to pass variables from a Rails application to JavaScript. Here I show three techniques: a script tag, a data attribute, and the Gon gem.
- Download:
- source code
- mp4
- m4v
- webm
- ogv



For the routes, I use the js-routes gem quite extensively, it has always worked well for me.
Really happy to see Gon+RABL covered in this screencast. That has been my favored approach to passing data to javascript lately. Thanks Alexey for making the Gon gem (and integrating it with RABL) and thanks to Ryan for another great screencast.
I second was Eric has said above.
We need deeper nuts and bolts level advice on integrating Rails with Coffeescript/backbones/Twitter Bootstrap/datatables (or a grid/table display widget) in a javascript (jQuery really) centric environment. There is a nice article published on Backbone/Twitter Bootstrap which Mike Gunderloy linked in his "A fresh cup" column today:
http://coenraets.org/blog/2012/02/sample-app-with-backbone-js-and-twitter-bootstrap/
If something like this can be done in a Rails environment, it gives us enough of a framework to create a nice look and feel.
+1 for Backbone/Rails/Twitter bootstrap - very cool
re: Coffeescript/Backbone/Datatables - you might want to check out Slickback - a plugin that integrates Backbone and Slickgrid.
I'm thinking about using this technique to create a pop up for login. But not too sure how to procede (haven't tried yet, just thinking). I do know I need to send json from session#new function and use js to consume that and add it to the hidden lightbox. That's how I'm thinking about it, can anyone let me know if this sounds right...I'd like to use gon and possibly rabl too. Help appreciated.
"<%=j products_url %>";
products_url wasn't set as a variable anywhere. How did the above code get the url? localhost:3000/products
Why was the "j" necessary?
products_url comes from the routes, and j is a method to escape the output so that it can be used with javascript
Thanks for the railscast, Gon looks very promising!
My only fear is that google won't index anything more then 'Loading products...', making this technique unusable for certain projects. Are there any solutions known for this problem?
Looks good but seems to have a conflict with cancan (both current release and 2.0 alpha). Would post a bug report but not really sure which gem is the culprit.
On a brand new rails 3.2.1 app with only cancan and rabl added to Gemfile:
/usr/local/rvm/gems/ruby-1.9.3-p125@***/gems/rabl-0.2.8/lib/rabl/template.rb:66:in `<top (required)>': uninitialized constant ActionView::TemplateHandlers (NameError) from /usr/local/rvm/gems/ruby-1.9.3-p125@***/gems/rabl-0.2.8/lib/rabl.rb:12:in `register!' from /usr/local/rvm/gems/ruby-1.9.3-p125@***/gems/rabl-0.2.8/lib/rabl/railtie.rb:6:in `block (2 levels) in <class:Railtie> <snip>'Anyone else seeing this?
Yes. I also have this issue. Rails 3.2.1 with rabl.
I am getting this error as well, though without cancan. Same trace to rabl/template.rb:66...
define in your gemfile that you need '0.6.0'. apparently they released a new version and somehow my gemfile thought it had to use 0.2.8. Which clearly wasn't compatible yet with rails 3.2.
one problem with using javascript tags is the 'j' escape function in rails doesn't work correctly or doesn't work how you might expect it to work. the string you send it is not always preserved and is corrupted in subtle ways.
for example if you have a string like:
and if you have:
then window.myvar == "<foo>"
the j function really has to replace '<' and '>' with '\u003C' and '\u003E'.
that should be:
then
Does gon work with jruby-1.6.5 ? Thanks
travis-ci told me that it works on all rubies, jrubies and rbxes
So gon did work for jruby. and I can alert the gon object from inside my coffeescript as a string. But when I assign the gon object(which is a json object) to my map, it does not pick up the data. I tried using the javascript tag and send a json object in the same way, and that works !
I'm trying to refresh a kendo chart with new data that uses gon variables as the data......it doesn't refresh the gon variable though with an ajax call! Any suggestions? I looked at gon.watch, but I need to update mutliple variables and it seems to work best with just a single variable and a time interval. I just need to refresh all the gon variables I have on the ajax call.
I'm trying to refresh a kendo chart with new data that uses gon variables as the data......it doesn't refresh the gon variable though with an ajax call! Any suggestions? I looked at gon.watch, but I need to update mutliple variables and it seems to work best with just a single variable and a time interval. I just need to refresh all the gon variables I have on the ajax call.
Does Gon work on production, when asset pipeline is on and js minified?
An if it does, how it works? Does it re-minified applications.js, I thought that application.js was cached through all requests.
Thanks,
alfredo
Exactly what I was looking for! Thanks.
Does anybody know how to use Gon in helper methods?
I have the same problem of benniemietz, gon variables doesn't refresh after ajax call, I tried with gon.watch, reset and reload without success. Anybody has a suggestion?
First sign in through GitHub to post a comment.