我有这个错误控制器:
class ErrorsController < ApplicationController
def error_404
@not_found_path = params[:not_found]
@return_page = request.referer || root_path
errors_respond 404
end
def error_500
errors_respond 500
end
end
在我的applicaiton_controller.rb
我有
def render_error(status, exception)
errors_respond status
rescue
end
render_error 方法在application_helper.rb
-file中定义
def errors_respond(status)
respond_to do |format|
format.html { render :template => "errors/error_#{status}", :layout => 'layouts/application_bootstrap', :status => status }
format.all { render :nothing => true, :status => status }
end
end
routes.rb
在我的-file的最底部,我有
match '/i_really_do_not_exist', to: redirect('/') # WTF? error_controller_specs will fail if this is removed
match '*not_found', to: 'errors#error_404'
提到error_controller_spec.rb
的是这个
require 'spec_helper'
describe ErrorsController do
describe "GET 'error_404'" do
it "returns http status 404" do
get 'error_404'
response.response_code.should == 404
end
end
describe "GET 'error_500'" do
it "returns http status 500" do
get 'error_500'
response.response_code.should == 500
end
end
end
如果我在没有'/i_really_do_not_exist'
路径的情况下运行它们,它们会失败
ActionController::RoutingError:
No route matches {:controller=>"errors", :action=>"error_404"}
如果我删除了/
.
我可以更改匹配部分和重定向部分,并且我的规范将通过,但是如果我完全删除它,它们会失败。
生成的路由match '/i_really_do_not_exist' => redirect('/')
是:controller#:action
.
有谁知道怎么回事?