Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Rails view template handler that renders Ruby objects to JSON.
Ruby
Branch: master

README.rdoc

ruby_template_handler

DESCRIPTION:

A Rails view template handler that renders Ruby objects to JSON.

FEATURES/PROBLEMS:

  • I'm not entirely sure how sensible this whole thing is, but there you go.

SYNOPSIS:

A controller action for returning a list of Movie objects in JSON format

# app/controllers/movies_controller.rb
def index
  respond_to do |format|
    format.json do
      @movies = Movie.all
      @count = Movie.count
      render :template => 'movies/index.json.rb'
    end
  end
end

# app/views/movies/index.json.rb
{
  :identifier => Movie.primary_key,
  :totalCount => @count,
  # render @movies does not work as it insists on returning a string
  :items => @movies.map { |m| render(m) }
}

# app/views/movies/_movie.json.rb
{
  :id => movie.to_param,
  :title => movie.title,
  :releaseDate => movie.release_date
}

In general, “full” templates are rendered to strings, whereas partials are rendered as objects.

REQUIREMENTS:

  • Rails

INSTALL:

  • sudo gem install mschuerig-ruby_template_handler

LICENSE:

(The MIT License)

Copyright © 2009 Michael Schuerig

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.