I spent a couple of hours trying to figure out how to handle 404 and 500 errors in Rails. This is not simple and actually really annoying. Hopefully future versions clean this up because right now it sucks pretty badly. Anyways, I found a page on the wiki and some other blogs, but the issue was that they wouldn’t handle all the cases. So, here’s the solution:
1. Edit your app/controllers/application.rb file and add these three methods:
def rescue_404 rescue_action_in_public CustomNotFoundError.new end def rescue_action_in_public(exception) case exception when CustomNotFoundError, ::ActionController::UnknownAction then #render_with_layout "shared/error404", 404, "standard" render :template => "shared/error404", :layout => "standard", :status => "404" else @message = exception render :template => "shared/error", :layout => "standard", :status => "500" end end def local_request? return false end
The first method will be explained in the next step. The second method is the method that Rails calls to handle most errors. This method will not capture a certain class of errors where neither the controller nor the action requested exist. The third method tells rails to stop sucking. Normally Rails handles requests made to localhost or 127.0.0.1 differently than all others. This might be for debugging purposes, but it sucks when testing error handling.
2. Edit config/routes.rb and add this line TO THE END OF THE FILE:
map.connect '*path', :controller => 'application', :action => 'rescue_404' unless ::ActionController::Base.consider_all_requests_local
This tells Rails that if it can’t find any other route to handle the request (i.e. the *path) it should call the rescue_404 action on the application controller (the first method above).
3. Edit config/environments/development.rb and add this line:
ActionController::Base.consider_all_requests_local = false
This additionally tells Rails to stop sucking and stop handling requests to localhost and 127.0.0.1 differently.
Anyways, happy coding.
相关推荐
You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...
This concise teaches you how to use the power of Docker and Kubernetes to deploy your Rails applications easily and efficiently. Docker and Kubernetes are increasing in popularity every day, but what ...
RUBY的经典之作,对其在RAILS下开发写得很详细
Ruby on Rails is the revolutionary online programming tool that makes creating functional e-commerce web sites faster and easier than ever. With the intuitive, straightforward nature of Ruby and the ...
You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...
Testing lessons show you how to eliminate cross-browser JavaScript errors and DOM debugging nightmares using a combination of Firebug, and Venkman. Advanced material explains the most current design ...
Ruby on Rails and J2EE
CoffeeScript Programming with jQuery, Rails, and Node.js.pdf 下载
This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...
Apress,.Beginning.Google.Maps.Applications.with.Rails.and.Ajax.
Your Ruby on Rails ...This new edition has been updated to Rails 5.2 and RSpec 3.7 and contains full coverage of new Rails features, including system tests and the Webpack-based JavaScript setup.
Rails, Angular, Postgres, and Bootstrap(2nd),电子书实战版
NetBeans Ruby and Rails IDE with JRuby 2009,大家看标题就知道了啊。。好东西啊,刚得到就发上来了。。
This short early adopter book details both how you’ll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...
rails-angular-postgres-and-bootstrap-second-edition 英文原版
and Images in a Rails Application 55 Setting up 56 Styling typography 57 Aligning text 61 Text transformation 62 Blockquotes 62 Styling listing elements 64 Creating and styling buttons 67 Which ...
Ruby on Rails: Up and Running
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...