Rails 3 Dynamic Error Pages
Contents |
here for a quick overview of the site Help Center Detailed answers rails error page to any questions you might have Meta Discuss the workings
Rails Exceptions_app
and policies of this site About Us Learn more about Stack Overflow the company Business
Rails Render 404 Page
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Rails 404 Page With Layout
Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Dynamic error pages in Rails 3 up vote 37 down vote favorite 21 In Rails 2.3.x, you can override render_optional_error_file like so: # ApplicationController.rb rails 4 exceptions_app protected def render_optional_error_file(status_code) render :template => "errors/500", :status => 500, :layout => 'application' end However, Rails 3 no longer has render_optional_error_file. Instead, you need to override rescue_action_in_public, which you can do like so: # config/initializers/error_page.rb module ActionDispatch class ShowExceptions protected def rescue_action_in_public(exception) status = status_code(exception).to_s template = ActionView::Base.new(["#{Rails.root}/app/views"]) if ["404"].include?(status) file = "/errors/404.html.erb" else file = "/errors/500.html.erb" end body = template.render(:file => file) render(status, body) end end end This works, but does not use the application's layout. However, if you specify the layout path like so: body = template.render(:file => file, :layout => "layouts/application") # line 15 You get an Error during failsafe response: ActionView::Template::Error. Line 4 of application.html.erb:4 is: <%= stylesheet_link_tag "app", "jquery-ui", "jquery.fancybox", :cache => "all" %> Whatever ActionView normally uses to render templates isn't getting loaded. The stack trace is: /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `join' /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `rails_asset_id' /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:817:in `rewrite_asset_path' /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:746:in `compute_public_path' /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:424:in `path_to_stylesheet' /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:875:in `ensure_stylesheet_sources!' /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `each' /v
You may have noticed the 404.html, 422.html and 500.html files that are generated with every new Rails project and rails error page gem wondered if there's a clean way to style them like the rails 404 route rest of your application. There is, and it's surprisingly simple. Basic Implementation The default status code templates rails 500 error are served by a Rack exception application. You can override this to be any Rack compatible app, including your applications router: # config/application.rb config.exceptions_app = self.routes This will route http://stackoverflow.com/questions/5331008/dynamic-error-pages-in-rails-3 any exceptions caught to your router Rack app. Now you'll want to define routes to display those errors yourself: # config/routes.rb get "/404", :to => "errors#not_found" get "/422", :to => "errors#unacceptable" get "/500", :to => "errors#internal_error" This will route each error code to it's respective action in ErrorsController. Now we'll want to define those actions: class ErrorsController < https://wearestac.com/blog/dynamic-error-pages-in-rails ApplicationController def not_found render :status => 404 end def unacceptable render :status => 422 end def internal_error render :status => 500 end end We tell each action to render the appropriate HTTP status code related to the error that's been caught. All that's left to do now is create the view related to each action and you're done: # app/views/errors/not_found.html.haml %h1 404 - Not Found When we visit /404 our 404 - Not Found 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. Optimising Our Errors Controller So far we've got working error pages, but it doesn't feel like the most DRY implementation. We could make it more RESTful by refactoring our errors controller to use a show action instead. Let's start by changing our routes: # config/routes.rb %w( 404 422 500 ).each do |code| get code, :to => "errors#show", :code => code end Now we need to ensure our ErrorsController uses the code parame
Sign in Pricing Blog Support Search GitHub This repository Watch 2 Star 5 Fork 1 kwbock/dynamic_errors Code Issues 0 Pull requests 0 Projects 0 Pulse Graphs Dynamic error pages in Rails 25 commits 1 https://github.com/kwbock/dynamic_errors branch 4 releases Fetching contributors MIT Ruby 100.0% Ruby Clone or download Clone with HTTPS Use Git or checkout with SVN using the web URL. Open in Desktop Download ZIP Find file Branch: master Switch branches/tags Branches Tags master Nothing to show v0.2.0 v0.1.2 v0.1.1 help Nothing to show New pull request Fetching latest commit… Cannot retrieve the latest commit at this time. Permalink Failed to load latest commit error page information. app/views/errors lib test .gitignore Gemfile Gemfile.lock LICENSE.txt README.rdoc Rakefile VERSION dynamic_errors.gemspec README.rdoc dynamic_errors Currently this gem is fairly broken. We are working on updating it to work with the latest minor version of Rails (at the time of this post, 3.2.x). Thank you for your patience. This gem wraps the standard Rails 3 error pages (public/(404|422|500).html) with the applications layout. Any further changes to the application layout will be rails error page reflected on the error pages, preventing having to make layout changes in three places. Installation gem install dynamic_errors Usage add “require 'dynamic_errors'” to environment.rb Create the following views if you wish to override the standard error message errors/404.html.erb errors/422.html.erb errors/500.html.erb Compatibility Currently the gem has only been tested (very briefly at that) with Rails -v3.0.3-7. Rails 2.x may be added if requested. Contributing to dynamic_errors Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it Fork the project Start a feature/bugfix branch Commit and push until you are happy with your contribution Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. Copyright Copyright © 2011 Kyle Bock. See LICENSE.txt for further details. Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't per