From 2abdf89511dd941d60444e7c08b8421b376c86e3 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Wed, 23 Nov 2011 21:46:18 +0100 Subject: [PATCH] 404 handling --- app/assets/javascripts/exceptions.js.coffee | 3 +++ app/assets/stylesheets/exceptions.css.scss | 3 +++ app/controllers/application_controller.rb | 4 ++++ app/controllers/exceptions_controller.rb | 10 ++++++++++ app/helpers/exceptions_helper.rb | 2 ++ app/views/exceptions/not_found.html.erb | 1 + config/routes.rb | 2 ++ spec/controllers/exceptions_controller_spec.rb | 5 +++++ spec/helpers/exceptions_helper_spec.rb | 15 +++++++++++++++ 9 files changed, 45 insertions(+) create mode 100644 app/assets/javascripts/exceptions.js.coffee create mode 100644 app/assets/stylesheets/exceptions.css.scss create mode 100644 app/controllers/exceptions_controller.rb create mode 100644 app/helpers/exceptions_helper.rb create mode 100644 app/views/exceptions/not_found.html.erb create mode 100644 spec/controllers/exceptions_controller_spec.rb create mode 100644 spec/helpers/exceptions_helper_spec.rb diff --git a/app/assets/javascripts/exceptions.js.coffee b/app/assets/javascripts/exceptions.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/exceptions.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/exceptions.css.scss b/app/assets/stylesheets/exceptions.css.scss new file mode 100644 index 0000000..8dd67b1 --- /dev/null +++ b/app/assets/stylesheets/exceptions.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the exceptions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e8065d9..5a09c28 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,7 @@ +class NotFound < StandardError; end + class ApplicationController < ActionController::Base protect_from_forgery + rescue_from(NotFound) { render 'exceptions/not_found' } + end diff --git a/app/controllers/exceptions_controller.rb b/app/controllers/exceptions_controller.rb new file mode 100644 index 0000000..65928fc --- /dev/null +++ b/app/controllers/exceptions_controller.rb @@ -0,0 +1,10 @@ +class ExceptionsController < ApplicationController + + def not_found + respond_to do |format| + format.any { render :text => 'Requested resource not found', :status => 404 } + format.html { render :status => 404 } + end + end + +end diff --git a/app/helpers/exceptions_helper.rb b/app/helpers/exceptions_helper.rb new file mode 100644 index 0000000..dcae663 --- /dev/null +++ b/app/helpers/exceptions_helper.rb @@ -0,0 +1,2 @@ +module ExceptionsHelper +end diff --git a/app/views/exceptions/not_found.html.erb b/app/views/exceptions/not_found.html.erb new file mode 100644 index 0000000..f06ab3d --- /dev/null +++ b/app/views/exceptions/not_found.html.erb @@ -0,0 +1 @@ +404 dude diff --git a/config/routes.rb b/config/routes.rb index db47880..74765ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -55,4 +55,6 @@ AsciiIo::Application.routes.draw do # This is a legacy wild controller route that's not recommended for RESTful applications. # Note: This route will make all actions in every controller accessible via GET requests. # match ':controller(/:action(/:id(.:format)))' + + match "*a", :to => "exceptions#not_found" end diff --git a/spec/controllers/exceptions_controller_spec.rb b/spec/controllers/exceptions_controller_spec.rb new file mode 100644 index 0000000..bac74fa --- /dev/null +++ b/spec/controllers/exceptions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe ExceptionsController do + +end diff --git a/spec/helpers/exceptions_helper_spec.rb b/spec/helpers/exceptions_helper_spec.rb new file mode 100644 index 0000000..b9ed048 --- /dev/null +++ b/spec/helpers/exceptions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the ExceptionsHelper. For example: +# +# describe ExceptionsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe ExceptionsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end