public
Description: A Rails plugin to use exceptions for generating HTTP status responses.
Homepage: http://wiki.github.com/wvanbergen/http_status_exceptions
Clone URL: git://github.com/wvanbergen/http_status_exceptions.git
wvanbergen (author)
Tue Sep 29 21:59:42 -0700 2009
commit  69a60aa57f14f48242bb41159a98512f1552f40d
tree    1b0896e8f5410fa83aa8a44de52a1a528f53f582
parent  d81f0845da7db5e37048e985a123cb3e31e144f8
name age message
file .gitignore Thu Jan 29 06:21:44 -0800 2009 Add directories to gitignore [wvanbergen]
file MIT-LICENSE Loading commit data...
file README.rdoc
file Rakefile
file http_status_exceptions.gemspec
file init.rb Sat Sep 20 09:58:15 -0700 2008 Initial check-in [wvanbergen]
directory lib/
directory spec/
directory tasks/
README.rdoc

WARNING: the gem version that Github currently serves is faulty. The issue is already fixes in the repository, but Github is not yet building new gem versions. As soon as this is fixed, I will release a new version that resolves the issue. For now, install version 0.1.5 which is unaffected:

  sudo gem install wvanbergen-http_status_exceptions \
      --source http://gems.github.com --version "= 0.1.5"

HTTP status exception

This simple plugin will register exception classes for all HTTP status. These exceptions can then be raised from your controllers, after which a response will be send back to the client with the desired HTTP status, possible with some other content.

You can use this plugin to access control mechanisms. You can simply raise a HTTPStatus::Forbidden if a user is not allowed to perform a certain action. A nice looking error page will be the result. See the example below:

See the project wiki (github.com/wvanbergen/http_status_exceptions/wikis) for additional documentation.

Installation

Installation is simple. Simply add the gem in your environment.rb:

  Rails::Initializer.run do |config|
    ...
    config.gem 'wvanbergen-http_status_exceptions', :lib => 'http_status_exceptions', :source => 'http://gems.github.com'
  end

Run rake gems:install to install the gem if needed.

Configuration

You can modify where HTTP status exception looks for its template files like so:

  class ApplicationController < ActionController::Base
    ...
    HTTPStatus::Base.template_path = 'path_to/http_status_templates'
  end

You can also modify which layout is used when rendering a template by setting the template_layout:

  class ApplicationController < ActionController::Base
    ...
    HTTPStatus::Base.template_layout = 'exception'
  end

If you don’t set a template_layout the current layout for the requested action will be used.

Usage

  class BlogController < ApplicationController

    def destroy
      raise HTTPStatus::Forbidden, 'You cannot delete blogs!' unless current_user.can_delete_blogs?
      @blog.destroy
    end
  end

By default, this will return an empty response with the "forbidden" status code (403). If you want to add content to the response as well, create the following view: shared/http_status/forbidden.html.erb. You can use the @exception-object in your view:

  <h1>Forbidden</h1>
  <p> <%= h(@exception.message) %> </p>
  <hr />
  <p>HTTP status code <small> <%= @exception.status_code %>: <%= @exception.status.to_s.humanize %></small></p>

The response will only be sent if the request format is HTML because of the name of the view file. In theory you could make a response for XML requests as well by using shared/http_status/forbidden.xml.builder as filename