我们有一个使用本教程的自定义404和500页设置的rails服务器:
http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
虽然它很好用并且为所有类型的路径抛出404,但它在尝试访问任何类型的后缀路径时会产生内部服务器错误500,如en/foo.png,en/foo.pdf,en/foo.xml,...
但是像en/file.foo这样的东西会抛出404.所以只有有效的后缀会抛出500.
routes.rb结束:
if Rails.application.config.consider_all_requests_local
match '*not_found', to: 'errors#error_404'
end
Run Code Online (Sandbox Code Playgroud)
application_controller.rb
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: :render_500
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ::AbstractController::ActionNotFound, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end
protected
def render_404(exception)
@not_found_path = exception.message
respond_to do |format|
format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
def render_500(exception)
logger.fatal(exception)
respond_to do |format|
format.html { render template: 'errors/error_500', layout: 'layouts/application', status: 500 …Run Code Online (Sandbox Code Playgroud) 我们遇到了一个奇怪的问题.在应用程序控制器中,我们有一个before_filter需要使用身份验证的集合,devise如果需要,还可以重定向到登录页面.
在我们的磁带库控制器,我们跳过这个before_filter.
skip_before_filter :authenticate_user!, :only => :show
Run Code Online (Sandbox Code Playgroud)
当我们运行简单的功能测试时capybara,rspec测试失败.
it "should get only english articles within the category 'computers'" do
visit '/en/library/computers'
page.should have_content('computers')
end
Run Code Online (Sandbox Code Playgroud)
看起来它不会跳过此过滤器.页面内容是登录页面.
当我们运行它时rails server它工作正常.
任何想法为什么它以这种方式行事或寻找什么来解决这个问题?
更新:
值得补充的是,这只发生在Linux上.在具有"相同"设置的MacOS 10.7下,它可以正常工作.
控制器代码:
class Library::CategoriesController < ApplicationController
skip_before_filter :authenticate_user!, :only => [:show]
# GET /categories/1
# GET /categories/1.json
def show
@categories = Category.all
@category = Category.find(params[:id])
@articles = @category.articles.where(:locale => I18n.locale)
respond_to do |format|
format.html # show.html.erb
end
end …Run Code Online (Sandbox Code Playgroud)