【发布时间】:2013-10-11 10:11:34
【问题描述】:
我有这个错误控制器:
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。
有人知道怎么回事吗?
【问题讨论】:
-
您是否尝试过比较
rake routes输出与没有这些行? -
我编辑了问题以包含
/i_really_do_not_exist的路线,即:controller#:action。
标签: ruby-on-rails rspec routes