我有这种错误的控制器:怪异情况下与路线和error_controller
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
的最底部 - 文件我有
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
。
任何人都知道发生了什么事?
您是否尝试过用与不用这些线路比较'''耙routes'''输出? – jcm
我编辑了问题以包含'/ i_really_do_not_exist'的路由,它是':controller#:action'。 –