-
-
Notifications
You must be signed in to change notification settings - Fork 183
Usage example
webmstk edited this page Mar 3, 2017
·
10 revisions
- Place include tag in html head
app/views/layouts/application.html.erb
<head>
<title>some title</title>
<%= Gon::Base.render_data %>
<!-- include your action js code -->
...
For rails 3:
<%= include_gon %>
...
- Assign variables in controller
@your_int = 123
@your_array = [1,2]
@your_hash = {'a' => 1, 'b' => 2}
gon.your_int = @your_int
gon.your_other_int = 345 + gon.your_int
gon.your_array = @your_array
gon.your_array << gon.your_int
gon.your_hash = @your_hash
gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
gon.your_array # > [1, 2, 123]
gon.clear # gon.all_variables now is {}
- Access the variables from js file
alert(gon.your_int)
alert(gon.your_other_int)
alert(gon.your_array)
alert(gon.your_hash)
For an excellent screencast of gon in action, check out Ryan Bates' Railscast "Passing Data to Javascript"