Click here to load reader
Upload
prograilscom
View
324
Download
4
Embed Size (px)
Citation preview
Passing Rails Variables To
Javascriptby Jedrzej Wiśniewski
How can I do it?
pass variable as data in html element
put js script in view
use gon gem
Pass variable in html element
In HTML
#my-element{ data: { troll: @troll } }
In JS
var troll = $(‘#my-element’).data(‘troll’)
Put JS script in view
In HTML
:javascript
var troll = “#{j
@troll}”
Using Gon gem
In Gemfile
gem ‘gon’
In application.html.haml
= include_gon
In controller
gon.troll = @troll
In JS
var troll = gon.troll