Giter Club home page Giter Club logo

http_status_exceptions's Introduction

WARNING: the gem version that Github currently serves is faulty. The issue is already fixed in the repository, but Github will not build new gems anymore. Make sure to install the latest version from the gemcutter repository (see below)

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 to the configuration in your environment.rb:

Rails::Initializer.run do |config|
  ...
  config.gem 'http_status_exceptions', :source => 'http://gemcutter.org'
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

http_status_exceptions's People

Contributors

wvanbergen avatar jamesbebbington avatar

Stargazers

Kevin Bullock avatar  avatar

Watchers

Kevin Bullock avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.