RubyTemplate gives you a simple means of producing json in your views by letting you write normal ruby. The return value of your template will be passed through ActiveSupport::JSON.encode to produce the actual json.


Add this line to your application's Gemfile:

gem 'ruby_template'

And then execute:

$ bundle


The template users/show.json.rb:

  :id   =>,
  :name =>,

will become:

{"user" : {"id" : 123, "name" : "Scott"}}

Simple as that.


Partials work mostly as expected:

# in (users/show.json.rb)
render(:partial => "users/user", object: @user)
# in (users/_user.json.rb)
  :id   =>,
  :name =>,

Note: when rendering partials, the return value is not a string (as normal), but instead we pass up the return value of the template.

Extending partials using deep_merge

It's nice to be DRY with your templates. You can use deep_merge to extend a common partial in contexts where extra information is needed. For example, say we want to include some private information after a user is created:

# in (users/create.json.rb)
render(:partial => "users/user", object: @user).deep_merge({
  :token => @user.


Rendering a partial with the collection option does not work. Instead, use:

# in (users/index.json.rb)
  :users =>{|u| render(:partial => "users/user", object: u)}


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request