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 ...
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 ...
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 ...
标题《Rails3 device and cancan》与描述《ROR ruby on rails device plugin教程》指出本文是关于如何在Rails 3.2应用程序中整合Devise认证插件和Cancan授权插件的教程。Devise是一个流行的Ruby on Rails的认证解决...
通过以上分析,我们可以看出,《CoffeeScript Programming with jQuery, Rails, and Node.js》这本书涵盖了CoffeeScript在Web开发领域的广泛应用,不仅包括了与主流前端库jQuery的结合,还深入探讨了其在Ruby on ...
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.
从给定的文件信息来看,我们正在探讨的是一本关于Ruby on Rails的书籍,书名为《Simply Rails2》,作者是Patrick Lenz。本书旨在为初学者提供深入理解Ruby on Rails框架的指南,从基础概念到高级主题均有涵盖,是...
### Rails 101 入门电子书知识点详解 #### 一、简介 《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别...
《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...
Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...
《Ruby on Rails: Up and Running》是一本针对初学者和有经验开发者的技术书籍,它深入浅出地介绍了如何使用Ruby on Rails框架构建Web应用程序。Ruby on Rails(简称Rails)是基于Ruby编程语言的一个开源Web应用框架...
### Ruby on Rails与J2EE:两者之间是否有共存的空间? #### 什么是Ruby on Rails? Ruby on Rails(简称Rails)是一种基于Ruby语言构建的相对新兴的Web应用框架。该框架被设计为现有企业级框架的一种替代方案,其...
Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...