In my routes.rb, I have the following:
%w(401 404 422 500 ).each do |code|
get code, :to => "errors#show", :code => code
end
This creates the following routes according to rake routes:
GET /401(.:format) errors#show {:code=>"401"}
GET /404(.:format) errors#show {:code=>"404"}
GET /422(.:format) errors#show {:code=>"422"}
GET /500(.:format) errors#show {:code=>"500"}
Here is my Errors Controller:
class ErrorsController < ApplicationController
def show
Rails.logger.debug("ERROR WITH STATUS #{status_code.to_s}")
render status_code.to_s, :status => status_code
end
def unauthorized
render "401"
end
protected
def status_code
Rails.logger.debug("STATUS CODE: #{params[:code]}")
params[:code] || 500
end
end
And in my views, I have the following pages under the folder "errors"
- 401.html.erb
- 404.html.erb
- 422.html.erb
- 500.html.erb
I'd like to redirect to an error page in a controller. If I wanted to redirect to 401, how would I do so?
I was able to solve the problem by adding this route to my routes.rb:
This calls the action "unauthorized" in my errors controller: