<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en">
  <id>tag:wearestac.com,2005:/blog</id>
  <link rel="alternate" type="text/html" href="http://wearestac.com" />
  
  <title>We Are Stac</title>
  <updated>2013-06-06T17:48:21Z</updated>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/wearestacblog" /><feedburner:info uri="wearestacblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <id>tag:wearestac.com,2005:Blog::Post/10</id>
    <published>2013-06-06T17:48:21Z</published>
    <updated>2013-06-07T11:09:17Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/wearestacblog/~3/id_9WsjjwvQ/recap-of-hey-stac-1" />
    <title>Recap of Hey!Stac #1</title>
    <content type="html">&lt;p&gt;Many thanks to everyone that attended our first Hey!Stac event on June the 4th. We hope you enjoyed it as much as we did. A special thanks to &lt;a href="https://twitter.com/csswizardry"&gt;Harry Roberts&lt;/a&gt; and &lt;a href="https://twitter.com/teabass"&gt;Andrew Nesbitt&lt;/a&gt; for their excellent talks, and to &lt;a href="https://twitter.com/welovetoeat"&gt;Louise Brogan-Hewitt&lt;/a&gt; for the awesome brownies!&lt;/p&gt;

&lt;p&gt;Harry Roberts kicked the night off with a &lt;a href="https://speakerdeck.com/csswizardry/css-youve-been-doing-it-wrong"&gt;talk&lt;/a&gt; explaining the benefits of designers adopting developer best practices. Josh and I then followed with a &lt;a href="https://speakerdeck.com/joshnesbitt/signal-box-burn-after-coding"&gt;presentation&lt;/a&gt; about our first (failed) attempt at creating a software startup and what we learnt from the experience. Finally, Andrew Nesbitt closed with &lt;a href="https://speakerdeck.com/andrew/nodecopter-cheltenham-geek-nights"&gt;slides&lt;/a&gt; and a set of demonstrations where he flew a quadcopter with various controllers via Node.js.&lt;/p&gt;

&lt;p&gt;We plan to make the night a regular meet up and will be confirming the date for the next one soon. Feel free to join us whether or not you attended the first time round, and if you&amp;#39;d like to give a talk on something that interests you please get in touch.&lt;/p&gt;

&lt;p&gt;You can see the photos from the evening on &lt;a href="http://www.flickr.com/photos/97147171@N08/8971283160/in/set-72157633972237888"&gt;Flickr&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Harry Roberts &lt;br&gt; &lt;a href="https://speakerdeck.com/csswizardry/css-youve-been-doing-it-wrong"&gt;CSS - You&amp;#39;ve Been Doing It Wrong&lt;/a&gt;&lt;/h3&gt;

&lt;script async class="speakerdeck-embed" data-id="2f8ba480af7f013006b37284e4fbe750" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;

&lt;h3&gt;Josh Nesbitt &amp;amp; Brent Murphy &lt;br&gt; &lt;a href="https://speakerdeck.com/joshnesbitt/signal-box-burn-after-coding"&gt;Signal Box - Burn After Coding&lt;/a&gt;&lt;/h3&gt;

&lt;script async class="speakerdeck-embed" data-id="9dff1750aff30130b71c2ea2a340f7fe" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;

&lt;h3&gt;Andrew Nesbitt &lt;br&gt; &lt;a href="https://speakerdeck.com/andrew/nodecopter-cheltenham-geek-nights"&gt;Node.js and Quadcopters - What could go wrong?&lt;/a&gt;&lt;/h3&gt;

&lt;script async class="speakerdeck-embed" data-id="68e15de08e120130c6ac123139337058" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;
&lt;img src="http://feeds.feedburner.com/~r/wearestacblog/~4/id_9WsjjwvQ" height="1" width="1"/&gt;</content>
    <author>
      <name>Brent</name>
    </author>
  <feedburner:origLink>http://wearestac.com/blog/recap-of-hey-stac-1</feedburner:origLink></entry>
  <entry>
    <id>tag:wearestac.com,2005:Blog::Post/9</id>
    <published>2013-05-15T13:33:21Z</published>
    <updated>2013-05-29T14:12:38Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/wearestacblog/~3/svvJAyqaCTo/introducing-hey-stac" />
    <title>Introducing Hey!Stac</title>
    <content type="html">&lt;p&gt;&lt;img src="http://wearestac-site-production.s3.amazonaws.com/blog/introducing-heystac/logo-small.png" title="Hey!Stac" style="margin: 40px auto; border: none; max-width: 292px; display: block;"&gt;&lt;/p&gt;

&lt;p&gt;We&amp;#39;re proud to announce a new series of talks and socials to take place over the coming months called &lt;strong&gt;Hey!Stac&lt;/strong&gt; held at The Faversham in Leeds, UK.&lt;/p&gt;

&lt;p&gt;Over the last few years we&amp;#39;ve been lucky enough to work with the most exciting, cutting edge technology. Our clients have trusted us to select the right tools for the job. We&amp;#39;ve also been fortunate enough to meet really talented individuals who are just as passionate about those technologies. We want to create a space where developers and other creatives can meet, talk shop and build a community. We hope to achieve this with Hey!Stac.&lt;/p&gt;

&lt;p&gt;This is where you come in! The first event is to be held on &lt;strong&gt;4th June&lt;/strong&gt;, with 3 speakers confirmed. We&amp;#39;d love for this to grow into a bigger community event, but we need your support.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s a little more information about the first event:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;3 speakers, each with a talk roughly around 15 minutes based on various topics from CSS to Quadcopters!&lt;/li&gt;
&lt;li&gt;The event is kindly hosted by The Faversham in Leeds, with plenty of comfortable chesterfield sofas and a well stocked bar&lt;/li&gt;
&lt;li&gt;The event opens at 7pm (talks will begin at 7:30pm)&lt;/li&gt;
&lt;li&gt;Following the event, attendees are welcome to join us for a meal in the centre of Leeds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We&amp;#39;ve posted the event to &lt;a href="http://lanyrd.com/2013/stactalks/"&gt;Lanyrd&lt;/a&gt; for people to register interest. If you&amp;#39;re thinking of coming drop us a &lt;a href="http://twitter.com/wearestac"&gt;tweet&lt;/a&gt;! We&amp;#39;re also looking for speakers for future events, if this sounds like something you&amp;#39;re interested in please &lt;a href="mailto:josh@wearestac.com"&gt;get in touch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We look forward to seeing you on 4th June!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/wearestacblog/~4/svvJAyqaCTo" height="1" width="1"/&gt;</content>
    <author>
      <name>Josh</name>
    </author>
  <feedburner:origLink>http://wearestac.com/blog/introducing-hey-stac</feedburner:origLink></entry>
  <entry>
    <id>tag:wearestac.com,2005:Blog::Post/8</id>
    <published>2013-04-25T09:00:14Z</published>
    <updated>2013-04-25T11:00:31Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/wearestacblog/~3/QSIsbZK_8W0/stac-client-receives-funding-for-expansion" />
    <title>Stac Client Receives Funding For Expansion</title>
    <content type="html">&lt;p&gt;For the past few months we&amp;#39;ve been working alongside &lt;a href="http://fatsoma.com"&gt;Fatsoma&lt;/a&gt;, an online ticketing company that enables users to purchase tickets and organise events with friends. &lt;a href="http://thehivemanchester.com/"&gt;Based in Manchester&lt;/a&gt; and founded in 2006, Fatsoma have &lt;a href="http://www.growthbusiness.co.uk/news-and-market-deals/fundraising-deals/2295083/laterooms-cofounder-backs-twitter-and-facebookbased-event-ticketing-platform-fatsoma.thtml"&gt;recently secured funding&lt;/a&gt; in order to add features and move into new markets.&lt;/p&gt;

&lt;p&gt;At Stac, we&amp;#39;re assisting the existing team in their drive to enhance the product and meet the demands of it&amp;#39;s growing user base. We look forward to more exciting months ahead and plan to publish some technical posts about our work with Fatsoma in the near future.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/wearestacblog/~4/QSIsbZK_8W0" height="1" width="1"/&gt;</content>
    <author>
      <name>Brent</name>
    </author>
  <feedburner:origLink>http://wearestac.com/blog/stac-client-receives-funding-for-expansion</feedburner:origLink></entry>
  <entry>
    <id>tag:wearestac.com,2005:Blog::Post/5</id>
    <published>2013-03-27T09:02:31Z</published>
    <updated>2013-03-27T09:13:57Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/wearestacblog/~3/lhoMDFiHWbs/raising-and-rescuing-custom-errors-in-rails" />
    <title>Raising and Rescuing Custom Errors in Rails</title>
    <content type="html">&lt;p&gt;Following on from our post on &lt;a href="http://wearestac.com/blog/dynamic-error-pages-in-rails"&gt;Dynamic Error Page in Rails&lt;/a&gt;, this week we&amp;#39;re going to look at raising and rescuing custom errors in a Rails application.&lt;/p&gt;

&lt;p&gt;It&amp;#39;s often useful to map custom Ruby errors to HTTP response status codes and have Rails render the appropriate HTML error pages. For example, you might have a controller that is acting as a simple proxy to a third party service such as Twitter or Facebook, and you need any of the HTTP errors encountered when calling those sites to be handled natively by your app. Another use case would be in a Service-oriented architecture (SOA), where you want any errors in your back end services propagated to your front end web application.&lt;/p&gt;

&lt;p&gt;In this post we&amp;#39;ll demonstrate rescuing status errors in an imaginary proxy controller using the awesome &lt;a href="https://github.com/lostisland/faraday"&gt;Faraday gem&lt;/a&gt;. For the sake of brevity we&amp;#39;ve omitted the inclusion of tests though in the wild we&amp;#39;d build such a feature using TDD and our favourite test weapon, RSpec.&lt;/p&gt;

&lt;h3&gt;Not Found&lt;/h3&gt;

&lt;p&gt;To start, let&amp;#39;s handle basic 404 Not Found errors that occur when calling a service. For this we&amp;#39;ll need a custom error class that extends &lt;code&gt;StandardError&lt;/code&gt;.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/not_found.rb
module Errors
  class NotFound &lt; StandardError; end
end
&lt;/pre&gt;

&lt;p&gt;Faraday provides a neat Rack-esque middleware feature. By creating our own custom middleware we can catch any Faraday 404s and raise our custom error. Furthermore, we can re-use the middleware anytime we need the same behaviour.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/raise_error.rb
module Errors
  class RaiseError &lt; Faraday::Response::Middleware

    def on_complete(env)
      raise Errors::NotFound if env[:status] == 404
    end

  end
end
&lt;/pre&gt;

&lt;p&gt;Now for the proxy controller.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# app/controllers/proxy_controller.rb
class ProxyController &lt; ApplicationController

  def index
    connection = Faraday.new(:url =&gt; 'http://someservice') do |f|
      f.adapter Faraday.default_adapter
      f.use     Errors::RaiseError       # Include custom middleware
    end

    response = connection.get('/some/resource')

    render :text =&gt; response.body
  end

end
&lt;/pre&gt;

&lt;p&gt;At this point any &lt;code&gt;NotFound&lt;/code&gt;s raised will still result in a 500 Internal Server Error in Rails. To alleviate this let&amp;#39;s create a module that uses &lt;a href="http://apidock.com/rails/ActionController/Rescue/ClassMethods/rescue_from"&gt;rescue_from&lt;/a&gt;, catches any custom &lt;code&gt;NotFound&lt;/code&gt;s and renders the default 404 page.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/rescue_error.rb
module Errors
  module RescueError

    def self.included(base)
      base.rescue_from Errors::NotFound do |e|
        render "public/404", :status =&gt; 404
      end
    end

  end
end
&lt;/pre&gt;

&lt;p&gt;We can then mixin &lt;code&gt;RescueError&lt;/code&gt; into our application controller and handle &lt;code&gt;NotFound&lt;/code&gt;s app-wide.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# app/controllers/application_controller.rb
class ApplicationController &lt; ActionController::Base
  include Errors::RescueError
end
&lt;/pre&gt;

&lt;h3&gt;Unprocessible Entity and Internal Server Error&lt;/h3&gt;

&lt;p&gt;Next, let&amp;#39;s create custom errors to help us manage proxy 422s and 500s.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/internal_server_error.rb
module Errors
  class InternalServerError &lt; StandardError; end
end

# lib/errors/unprocessable_entity.rb
module Errors
  class UnprocessableEntity &lt; StandardError; end
end
&lt;/pre&gt;

&lt;p&gt;We&amp;#39;ll also need to modify the &lt;code&gt;RaiseError&lt;/code&gt; middleware to raise those errors. Here, we also ignore any non-error response codes, and treat any unknown error responses as 500s.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/raise_error.rb
module Errors
  class RaiseError &lt; Faraday::Response::Middleware
    def on_complete(env)
      # Ignore any non-error response codes
      return if (status = env[:status]) &lt; 400
      case status
      when 404
        raise Errors::NotFound
      when 422
        raise Errors::UnprocessableEntity
      else
        raise Errors::InternalServerError # Treat any other errors as 500
      end
    end
  end
end
&lt;/pre&gt;

&lt;p&gt;Now, let&amp;#39;s change the &lt;code&gt;RescueError&lt;/code&gt; implementation to render the appropriate HTML page.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/rescue_error.rb
module Errors
  module RescueError

    def self.included(base)
      base.rescue_from Errors::NotFound do |e|
        render "public/404", :status =&gt; 404
      end
      base.rescue_from Errors::UnprocessableEntity do |e|
        render "public/422", :status =&gt; 422
      end
      base.rescue_from Errors::InternalServerError do |e|
        render "public/500", :status =&gt; 500
      end
    end

  end
end
&lt;/pre&gt;

&lt;h3&gt;Refactoring&lt;/h3&gt;

&lt;p&gt;So far so good. The code does what we need but there&amp;#39;s far too much duplication. Furthermore, if we want to map additional error codes we&amp;#39;ll have to add more branches to the switch statement in &lt;code&gt;RaiseError&lt;/code&gt; and more &lt;code&gt;rescue_from&lt;/code&gt; handlers to the &lt;code&gt;RescueError&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Accordingly, it&amp;#39;s time to refactor the existing code. Firstly, let&amp;#39;s delete our existing &lt;code&gt;StandardError&lt;/code&gt; subclasses, and create the equivalent classes using a sprinkling of metaprogramming.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors.rb
module Errors

  # Error constants
  NOT_FOUND               = 404
  UNPROCESSABLE_ENTITY    = 422
  INTERNAL_SERVER_ERROR   = 500

  # Base subclass for all response errors
  class ResponseError &lt; StandardError; end

  class &lt;&lt; self

    # Returns a hash of error names to response codes.
    def error_constants
      self.constants.each_with_object({}) do |name, hash|
        # Ignore any class constants
        next if (code = Errors.const_get(name)).is_a?(Class)
        hash[name] = code
      end
    end

    # Returns a class name from a constant name.
    def class_name_for_error_name(name)
      name.to_s.titleize.gsub(' ', '')
    end

    # Returns the error class for a given constant name.
    # Default to InternalServerError.
    def class_for_error_name(name)
      class_name = class_name_for_error_name(name)
      const_defined?(class_name) ? Errors.const_get(class_name) : Errors::InternalServerError
    end

    # Returns the error class for a given error code.
    # Default to InternalServerError.
    def class_for_error_code(code)
      name = error_constants.select { |k, v| v == code }.keys.first
      name.present? ? class_for_error_name(name) : Errors::InternalServerError
    end

  end

end

# Dynamically creates a subclass of ResponseError for each error constant.
# Adds a code method to return the correct response code.
# Adds the new class to the constants in the Errors module.
Errors.error_constants.each do |name, code|
  klass = Class.new(Errors::ResponseError)
  klass.send(:define_method, :code) { code }
  Errors.const_set(Errors.class_name_for_error_name(name), klass)
end
&lt;/pre&gt;

&lt;p&gt;This is a large refactoring, but permits us to vastly simplify the error raising code.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/raise_error.rb
module Errors
  class RaiseError &lt; Faraday::Response::Middleware

    def on_complete(env)
      return if (status = env[:status]) &lt; 400
      raise Errors.class_for_error_code(status)
    end

  end
end
&lt;/pre&gt;

&lt;p&gt;And similarly, the error rescuing code.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/rescue_error.rb
module Errors
  module RescueError

    def self.included(base)
      base.rescue_from Errors::ResponseError do |e|
        render "public/#{e.code}", :status =&gt; e.code
      end
    end

  end
end
&lt;/pre&gt;

&lt;p&gt;Now, raising and rescuing additional error types is as simple as adding new constants to the &lt;code&gt;errors.rb&lt;/code&gt; file.&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# lib/errors/rescue_error.rb
module Errors

  # Error constants
  BAD_REQUEST             = 400
  NOT_AUTHORIZED          = 401
  FORBIDDEN               = 403
  NOT_FOUND               = 404
  METHOD_NOT_ALLOWED      = 405
  BAD_FORMAT              = 406
  UNPROCESSABLE_ENTITY    = 422
  INTERNAL_SERVER_ERROR   = 500

  ...
end
&lt;/pre&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;This brings our recent posts on error handling in Rails to a close. Feel free to add any suggestions, questions or feedback in the comments section.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/wearestacblog/~4/lhoMDFiHWbs" height="1" width="1"/&gt;</content>
    <author>
      <name>Brent</name>
    </author>
  <feedburner:origLink>http://wearestac.com/blog/raising-and-rescuing-custom-errors-in-rails</feedburner:origLink></entry>
  <entry>
    <id>tag:wearestac.com,2005:Blog::Post/4</id>
    <published>2013-03-13T18:12:15Z</published>
    <updated>2013-04-25T08:48:57Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/wearestacblog/~3/wmWDWMf6vRY/dynamic-error-pages-in-rails" />
    <title>Dynamic Error Pages In Rails</title>
    <content type="html">&lt;p&gt;It&amp;#39;s a little known fact that you can easily substitute the default Rails HTML &lt;a href="/404"&gt;error pages&lt;/a&gt; with something more pleasant. You may have noticed the &lt;code&gt;404.html&lt;/code&gt;, &lt;code&gt;422.html&lt;/code&gt; and &lt;code&gt;500.html&lt;/code&gt; files that are generated with every new Rails project and wondered if there&amp;#39;s a clean way to style them like the rest of your application. There is, and it&amp;#39;s surprisingly simple.&lt;/p&gt;

&lt;h3&gt;Basic Implementation&lt;/h3&gt;

&lt;p&gt;The default status code templates are served by a &lt;a href="https://github.com/rails/rails/blob/cd9f7508df9485ea7ec66d0172c1d6bcfe7ed5a8/actionpack/lib/action_dispatch/middleware/public_exceptions.rb"&gt;Rack exception application&lt;/a&gt;. You can override this to be any Rack compatible app, including your applications router:&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# config/application.rb
config.exceptions_app = self.routes
&lt;/pre&gt;

&lt;p&gt;This will route any exceptions caught to your router Rack app. Now you&amp;#39;ll want to define routes to display those errors yourself:&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# config/routes.rb
get "/404", :to =&gt; "errors#not_found"
get "/422", :to =&gt; "errors#unacceptable"
get "/500", :to =&gt; "errors#internal_error"
&lt;/pre&gt;

&lt;p&gt;This will route each error code to it&amp;#39;s respective action in &lt;code&gt;ErrorsController&lt;/code&gt;. Now we&amp;#39;ll want to define those actions:&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
class ErrorsController &lt; ApplicationController

  def not_found
    render :status =&gt; 404
  end

  def unacceptable
    render :status =&gt; 422
  end

  def interal_error
    render :status =&gt; 500
  end

end
&lt;/pre&gt;

&lt;p&gt;We tell each action to render the appropriate HTTP status code related to the error that&amp;#39;s been caught. All that&amp;#39;s left to do now is create the view related to each action and you&amp;#39;re done:&lt;/p&gt;

&lt;pre name="code" class="brush: plain;"&gt;
# app/views/errors/not_found.html.haml
%h1 404 - Not Found
&lt;/pre&gt;

&lt;p&gt;When we visit &lt;code&gt;/404&lt;/code&gt; our &lt;code&gt;404 - Not Found&lt;/code&gt; view should render as expected. Now you can style your error pages without having to duplicate any styles into the public directory of your application.&lt;/p&gt;

&lt;h3&gt;Optimising Our Errors Controller&lt;/h3&gt;

&lt;p&gt;So far we&amp;#39;ve got working error pages, but it doesn&amp;#39;t feel like the most &lt;a href="http://en.wikipedia.org/wiki/Don&amp;#x27;t_repeat_yourself"&gt;DRY&lt;/a&gt; implementation. We could make it more &lt;a href="http://en.wikipedia.org/wiki/Representational_state_transfer"&gt;RESTful&lt;/a&gt; by refactoring our errors controller to use a &lt;code&gt;show&lt;/code&gt; action instead.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s start by changing our routes:&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
# config/routes.rb
%w( 404 422 500 ).each do |code|
  get code, :to =&gt; "errors#show", :code =&gt; code
end
&lt;/pre&gt;

&lt;p&gt;Now we need to ensure our &lt;code&gt;ErrorsController&lt;/code&gt; uses the &lt;code&gt;code&lt;/code&gt; parameter we&amp;#39;re passing through:&lt;/p&gt;

&lt;pre name="code" class="brush: ruby;"&gt;
class ErrorsController &lt; ApplicationController

  def show
    render status_code.to_s, :status =&gt; status_code
  end

protected

  def status_code
    params[:code] || 500
  end

end
&lt;/pre&gt;

&lt;p&gt;To clean things up even more I&amp;#39;ve created a &lt;code&gt;status_code&lt;/code&gt; method which defaults to a &lt;code&gt;500&lt;/code&gt;, this will protect any cases where we might not have the code present in the &lt;code&gt;params&lt;/code&gt; hash.&lt;/p&gt;

&lt;p&gt;The final alteration as part of this refactor is to rename our view files to use status codes rather than our previous naming scheme:&lt;/p&gt;

&lt;pre name="code" class="brush: plain;"&gt;
# app/views/errors/404.html.haml
%h1 404 - Not Found
&lt;/pre&gt;

&lt;p&gt;And that&amp;#39;s it. We&amp;#39;ve now got a reusable errors controller which is flexible enough for us to add new error types to in the future (by adding a new code to the error codes array and a corresponding view file).&lt;/p&gt;

&lt;p&gt;It&amp;#39;s worth noting that you shouldn&amp;#39;t be doing anything fancy in these views. The reason these pages are rendered is because something has most likely gone wrong in your application, so you should probably stray away from making calls to the database or performing more complex actions.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/wearestacblog/~4/wmWDWMf6vRY" height="1" width="1"/&gt;</content>
    <author>
      <name>Josh</name>
    </author>
  <feedburner:origLink>http://wearestac.com/blog/dynamic-error-pages-in-rails</feedburner:origLink></entry>
  <entry>
    <id>tag:wearestac.com,2005:Blog::Post/1</id>
    <published>2013-03-05T13:46:03Z</published>
    <updated>2013-03-05T13:46:03Z</updated>
    <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/wearestacblog/~3/9vGhb512-Mc/a-new-stac" />
    <title>A New Stac</title>
    <content type="html">&lt;p&gt;It&amp;#39;s been well over a year since we last blogged and our online activity has been far too sparse, but it&amp;#39;s not because we haven&amp;#39;t been busy!&lt;/p&gt;

&lt;p&gt;Stac was founded over two and a half years ago and during this time we&amp;#39;ve learnt a lot. Not just about our customers, but also about what we want to achieve as a company. We&amp;#39;ve worked on a wide variety of projects - from creating basic sites for small local businesses to building web applications for large companies. This has helped us identify our strengths.&lt;/p&gt;

&lt;p&gt;We&amp;#39;re bringing you a new Stac. We&amp;#39;re re-inventing ourselves by concentrating on the areas where we excel. As of today we will focus on providing consultancy, programming and project management to startups and established businesses. We&amp;#39;ve created our own product and worked on many more, so we know how an application should be built and how to integrate ourselves into teams.&lt;/p&gt;

&lt;p&gt;We&amp;#39;re really excited about Stac&amp;#39;s new direction and we&amp;#39;re looking forward to meeting more original, creative businesses whose ideas we can make a reality.&lt;/p&gt;

&lt;p&gt;We hope you&amp;#39;ll join us on this blog (and on &lt;a href="http://twitter.com/wearestac"&gt;Twitter&lt;/a&gt;) for a new set of posts about the technical and non-technical challenges we encounter in our new venture.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/wearestacblog/~4/9vGhb512-Mc" height="1" width="1"/&gt;</content>
    <author>
      <name>Josh</name>
    </author>
  <feedburner:origLink>http://wearestac.com/blog/a-new-stac</feedburner:origLink></entry>
</feed>
