|
3 | 3 |
|
4 | 4 | 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! |
5 | 5 |
|
6 | | -Now with [Rabl](https://github.com/nesquena/rabl) support! |
| 6 | +Now with [Jbuilder](https://github.com/rails/jbuilder) and [Rabl](https://github.com/nesquena/rabl) support! |
7 | 7 |
|
8 | 8 | ## An example of typical use |
9 | 9 |
|
@@ -202,18 +202,54 @@ gon mapping method: |
202 | 202 | gon.rabl 'path/to/rabl/file', :as => 'alias' |
203 | 203 | ``` |
204 | 204 |
|
| 205 | +## Usage with Jbuilder |
| 206 | +
|
| 207 | +Use gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https://guthub.com/nesquena/rabl): |
| 208 | +
|
| 209 | +Jbuilder works now only on Ruby 1.9+, so Gon support for Jbuilder works on 1.9+ only |
| 210 | +
|
| 211 | + 1. Create Jbuilder template. |
| 212 | +
|
| 213 | + `app/views/posts/index.json.jbuilder` |
| 214 | +
|
| 215 | + ``` jbuilder |
| 216 | + json.posts @posts, :id, :title, :body |
| 217 | + ``` |
| 218 | +
|
| 219 | + 2. In your controller you should map this template to gon. |
| 220 | +
|
| 221 | + ``` ruby |
| 222 | + def index |
| 223 | + # some controller logic |
| 224 | + @posts = Post.all |
| 225 | +
|
| 226 | + gon.jbuilder 'app/views/posts/index.json.jbuilder' |
| 227 | + # some controller logic |
| 228 | + end |
| 229 | + ``` |
| 230 | +In javascript file for view of this action write call to your variable: |
| 231 | +
|
| 232 | +``` js |
| 233 | +alert(gon.posts) |
| 234 | +alert(gon.posts[0]) |
| 235 | +alert(gon.posts[0].post.body) |
| 236 | +``` |
| 237 | +
|
| 238 | +P.s. If you didn't put include_gon tag in your html head area - it |
| 239 | +wouldn't work. You can read about this in common usage above. |
| 240 | +
|
205 | 241 | ## Installation |
206 | 242 |
|
207 | 243 | Puts this line into `Gemfile` then run `$ bundle`: |
208 | 244 |
|
209 | 245 | ``` ruby |
210 | | -gem 'gon', '2.0.3' |
| 246 | +gem 'gon', '2.0.4' |
211 | 247 | ``` |
212 | 248 |
|
213 | 249 | Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`: |
214 | 250 |
|
215 | 251 | ``` ruby |
216 | | -config.gem 'gon', :version => '2.0.3' |
| 252 | +config.gem 'gon', :version => '2.0.4' |
217 | 253 | ``` |
218 | 254 |
|
219 | 255 | Or manually install gon gem: `$ gem install gon` |
|
0 commit comments