https://github.com/gazay/gon
Your Rails variables in your JS
https://github.com/gazay/gon
Keywords from Contributors
activerecord activejob mvc rspec rubygems rack ruby-gem crash-reporting sinatra error-monitoring
Last synced: about 18 hours ago
JSON representation
Repository metadata
Your Rails variables in your JS
- Host: GitHub
- URL: https://github.com/gazay/gon
- Owner: gazay
- License: mit
- Created: 2011-05-09T15:26:18.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2026-01-11T04:24:30.000Z (about 2 months ago)
- Last Synced: 2026-02-22T22:34:02.488Z (9 days ago)
- Language: Ruby
- Homepage:
- Size: 902 KB
- Stars: 3,085
- Watchers: 50
- Forks: 187
- Open Issues: 40
- Releases: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
README.md
Gon gem — get your Rails variables in your js

If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!
Now you can easily renew data in your variables through ajax with gon.watch!
With Jbuilder, Rabl, and Rabl-Rails support!
For Sinatra available gon-sinatra.
For .Net MVC available port NGon.
For elixir Phoenix available PhoenixGon.
An example of typical use
Very good and detailed example and reasons to use is considered in railscast by Ryan Bates
When you need to send some start data from your controller to your js
you might be doing something like this:
- Write this data in controller(presenter/model) to some variable
- In view for this action you put this variable to some objects by data
attributes, or write js right in view - Then there can be two ways in js:
+ if you previously wrote data in data
attributes - you should parse this attributes and write data to some
js variable.
+ if you wrote js right in view (many frontenders would shame you for
that) - you just use data from this js - OK. - You can use your data in your js
And every time when you need to send some data from action to js you do this.
With gon you configure it firstly - just put in layout one tag, and add
gem line to your Gemfile and do the following:
- Write variables by
gon.variable_name = variable_value
# or new syntax
gon.push({
:user_id => 1,
:user_role => "admin"
})
gon.push(any_object) # any_object with respond_to? :each_pair
- In your js you get this by
gon.variable_name
- profit?
With the gon.watch feature you can easily renew data in gon variables!
Simply call gon.watch from your js file. It's super useful
in modern web applications!
Usage
More details about configuration and usage you can find in gon wiki
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 %>
...
You can pass some options
to render_data method.
You put something like this in the action of your 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 your JavaScript file:
alert(gon.your_int)
alert(gon.your_other_int)
alert(gon.your_array)
alert(gon.your_hash)
AMD compatible version: include_gon_amd
If your site uses AMD modules you can use the include_gon_amd helper to
include the variables and watch function as a module. Options are mostly
the same as for include_gon, except for namespace_check, which does
nothing and namespace, which is used as the name of the defined module.
The end result will look somewhat like the following:
define('yourNameSpace', [], function() {
var gon = {};
gon.yourVariable = yourValue;
// etc...
return gon;
});
A (very) simplified usage example:
app/views/layouts/application.html.erb
include_gon_amd namespace: 'data'
Some JavaScript module
define(['data'], function(data) {
alert(data.myVariable);
});
gon.watch - renew your data easily!
You can use gon for renewing your data without reloading pages and
writing long js functions! It's really great for some live values.
Supports gon.watch.rabl and gon.watch.jbuilder usage.
Instruction for
usage gon.watch.
Usage with Rabl
You can write your variables assign logic to templates with Rabl.
The way of writing Rabl templates is very clearly described in their repo.
Profit of using Rabl with gon:
- You can clean your controllers now!
- Work with database objects and collections clearly and easyly
- All power of Rabl
- You can still be lazy and don't use common way to transfer data in js
- And so on
Instruction for
usage gon with Rabl.
Usage with Rabl-Rails
gon.rabl works with rabl-rails. Learn to write RABL the rabl-rails way here.
Add gon and rabl-rails to your environment:
gem 'gon'
gem 'rabl-rails'
Define a rabl template using rabl-rails syntax:
#app/views/users/show.rabl
object :@user
attributes :id, :name, :email, :location
Call gon.rabl in your controller
#app/controllers/users_controller.rb
def show
@user = User.find(params[:id])
gon.rabl
end
Usage with Jbuilder
Use gon with Jbuilder as with Rabl:
Instruction for
usage gon with Jbuilder.
gon.global
You can use gon for sending your data to js from anywhere! It's really
great for some init data.
Instruction for
usage gon.global.
Speed up Gon
You can use any JSON Engine you want.
Gon uses MultiJson with autodetect mode, so all you need is just require your JSON library.
Current Maintainers
Special thanks
- @takiy33
- @brainopia
- @kossnocorp
- @ai
Original Author
License
The MIT License
Security Contact
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Owner metadata
- Name: Alex Gaziev
- Login: gazay
- Email:
- Kind: user
- Description: Team Lead, Kaggle Expert, Artist
- Website: https://www.sungaz.art
- Location: San Francisco, CA
- Twitter: gazay
- Company: @evilmartians
- Icon url: https://avatars.githubusercontent.com/u/29658?v=4
- Repositories: 162
- Last ynced at: 2023-04-10T09:42:01.715Z
- Profile URL: https://github.com/gazay
GitHub Events
Total
- Release event: 1
- Member event: 1
- Pull request event: 28
- Fork event: 7
- Issues event: 8
- Watch event: 38
- Issue comment event: 23
- Push event: 17
- Pull request review comment event: 5
- Pull request review event: 9
- Create event: 4
Last Year
- Release event: 1
- Member event: 1
- Pull request event: 27
- Fork event: 5
- Issues event: 5
- Watch event: 31
- Issue comment event: 22
- Push event: 17
- Pull request review comment event: 5
- Pull request review event: 9
- Create event: 4
Committers metadata
Last synced: 2 days ago
Total Commits: 406
Total Committers: 72
Avg Commits per committer: 5.639
Development Distribution Score (DDS): 0.466
Commits in past year: 33
Committers in past year: 3
Avg Commits per committer in past year: 11.0
Development Distribution Score (DDS) in past year: 0.212
| Name | Commits | |
|---|---|---|
| Alexey Gaziev | a****v@g****m | 217 |
| Shinichi Maeshima | n****t@g****m | 26 |
| takiy33 | t****3@g****m | 23 |
| takiy33 | t****3 | 21 |
| Vlad Bokov | r****m@m****u | 8 |
| Maksim Berjoza | t****n@g****m | 7 |
| krororo | k****7@g****m | 5 |
| Peter M. Goldstein | p****n@g****m | 5 |
| Hector Satre | m****n@g****m | 5 |
| Strech (Sergey Fedorov) | s****f@m****u | 4 |
| Edward Tsech | e****h@g****m | 3 |
| Arsen Gasparyan | b****1@g****m | 3 |
| Alex Rakoczy | s****r@g****m | 3 |
| Ellis Berner | e****r@g****m | 3 |
| John Therrell | j****l@g****m | 3 |
| novoi | i****v@p****m | 3 |
| Scott Yoder | y****b@g****m | 2 |
| Rafael Liu | r****u@g****m | 2 |
| Philippe Vaucher | p****e@s****h | 2 |
| Peter Schröder | p****l@g****m | 2 |
| Nicolas Leger | n****r | 2 |
| Graeme Longman | g****n@g****m | 2 |
| Tom Hallett | t****t@g****m | 2 |
| Ville Kukkonen | k****e@g****m | 2 |
| Will Cosgrove | w****l@w****m | 2 |
| Yuri Zubov | y****v@c****o | 2 |
| aekym | me@a****m | 2 |
| Akira Matsuda | r****e@d****p | 1 |
| Artur Sabrov | s****j@g****m | 1 |
| Andreas Salme | a****e@y****m | 1 |
| and 42 more... | ||
Committer domains:
- mail.ru: 2
- me.com: 2
- pega.com: 1
- stvs.ch: 1
- willcosgrove.com: 1
- cleverlabs.io: 1
- aekym.com: 1
- dio.jp: 1
- yesvideo.com: 1
- eventmag.ru: 1
- rigor.com: 1
- hashrocket.com: 1
- panoramaed.com: 1
- sergeyp.me: 1
- toptal.com: 1
- sharshenov.com: 1
- speee.jp: 1
- musclesound.com: 1
- gitter.im: 1
- ojab.ru: 1
- 4angle.com: 1
- ombulabs.com: 1
- striking.ly: 1
- naturaily.com: 1
- coswellproductions.com: 1
- khusnetdinov.ru: 1
Issue and Pull Request metadata
Last synced: 9 days ago
Total issues: 80
Total pull requests: 47
Average time to close issues: 11 months
Average time to close pull requests: 4 months
Total issue authors: 75
Total pull request authors: 30
Average comments per issue: 3.18
Average comments per pull request: 1.0
Merged pull request: 30
Bot issues: 0
Bot pull requests: 0
Past year issues: 1
Past year pull requests: 20
Past year average time to close issues: N/A
Past year average time to close pull requests: 10 days
Past year issue authors: 1
Past year pull request authors: 5
Past year average comments per issue: 0.0
Past year average comments per pull request: 0.4
Past year merged pull request: 11
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- pravi (3)
- justin808 (2)
- nov (2)
- RavenXce (2)
- brian-mann (1)
- didil (1)
- ggrillone (1)
- ertrzyiks (1)
- stoplion (1)
- nullnull (1)
- etagwerker (1)
- rubydesign (1)
- corporealfunk (1)
- devopsjourney1 (1)
- almog (1)
Top Pull Request Authors
- willnet (12)
- krororo (3)
- shogo-08020318 (2)
- yuri-zubov (2)
- dee-see (2)
- Faq (2)
- ojab (1)
- 513ry (1)
- amatsuda (1)
- etipton (1)
- vlazar (1)
- cristina23 (1)
- ertrzyiks (1)
- wilddima (1)
- khusnetdinov (1)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 12
-
Total downloads:
- rubygems: 123,944,186 total
- Total docker downloads: 922,597,416
- Total dependent packages: 26 (may contain duplicates)
- Total dependent repositories: 6,597 (may contain duplicates)
- Total versions: 179
- Total maintainers: 2
- Total advisories: 2
gem.coop: gon
If you need to send some data to your js files and you don't want to do this with long way trough views and parsing - use this force!
- Homepage: https://github.com/gazay/gon
- Documentation: http://www.rubydoc.info/gems/gon/
- Licenses: MIT
- Latest release: 7.0.0 (published about 2 months ago)
- Last Synced: 2026-03-01T15:31:38.887Z (2 days ago)
- Versions: 57
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 61,973,052 Total
- Docker Downloads: 461,298,708
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.171%
- Docker downloads count: 0.237%
- Downloads: 0.446%
- Maintainers (2)
- Advisories:
rubygems.org: gon
If you need to send some data to your js files and you don't want to do this with long way trough views and parsing - use this force!
- Homepage: https://github.com/gazay/gon
- Documentation: http://www.rubydoc.info/gems/gon/
- Licenses: MIT
- Latest release: 7.0.0 (published about 2 months ago)
- Last Synced: 2026-03-01T09:41:29.215Z (2 days ago)
- Versions: 57
- Dependent Packages: 26
- Dependent Repositories: 6,597
- Downloads: 61,971,134 Total
- Docker Downloads: 461,298,708
-
Rankings:
- Docker downloads count: 0.292%
- Dependent repos count: 0.396%
- Downloads: 0.468%
- Stargazers count: 0.652%
- Average: 0.751%
- Dependent packages count: 0.873%
- Forks count: 1.826%
- Maintainers (1)
- Advisories:
proxy.golang.org: github.com/gazay/gon
- Homepage:
- Documentation: https://pkg.go.dev/github.com/gazay/gon#section-documentation
- Licenses: mit
- Latest release: v7.0.0+incompatible (published about 2 months ago)
- Last Synced: 2026-02-28T23:00:55.248Z (3 days ago)
- Versions: 56
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 6.521%
- Average: 6.74%
- Dependent repos count: 6.959%
debian-10: ruby-gon
- Homepage: https://github.com/gazay/gon
- Documentation: https://packages.debian.org/buster/ruby-gon
- Licenses:
- Latest release: 6.2.1-1 (published 20 days ago)
- Last Synced: 2026-02-13T04:21:55.462Z (19 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-11: ruby-gon
- Homepage: https://github.com/gazay/gon
- Documentation: https://packages.debian.org/bullseye/ruby-gon
- Licenses:
- Latest release: 6.4.0-1 (published 21 days ago)
- Last Synced: 2026-02-13T08:20:40.409Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-12: ruby-gon
- Homepage: https://github.com/gazay/gon
- Documentation: https://packages.debian.org/bookworm/ruby-gon
- Licenses:
- Latest release: 6.4.0-2 (published 19 days ago)
- Last Synced: 2026-02-12T23:30:20.138Z (19 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
Dependencies
- jbuilder >= 0 development
- pry >= 0 development
- pry-byebug >= 0 development
- rabl = 0.11.3 development
- rabl-rails >= 0 development
- railties >= 3.0.20 development
- rake >= 0 development
- rspec >= 3.0 development
- actionpack >= 3.0.20
- i18n >= 0.7
- multi_json >= 0
- request_store >= 1.0
- actions/checkout v5 composite
- ruby/setup-ruby v1 composite
Score: 33.09261924627365