更新
稍微想了想。如果您只有几种类型的错误,那么这样做怎么样?
在你routes.rb
的最后一行,添加一个
match '/my_segment/*path', :to => 'errors#not_found'
这应该匹配任何未定义的路径(通常会抛出ActionController::RoutingError
)并将其推送到您的全局错误页面。
您可以使用上面的段通配符玩游戏以获得正确的路径。这不应该影响您的预定义路径,例如mydomain.com/controller1
.
下面是一种更细粒度的控制方法。
这将帮助您匹配来自mydomain.com/some_controller/bad_params
def firstController < ApplicationController
def method_in_first_controller
# Do something here
rescue
@error = # Error object here
render :template=>"some_error_template", :status => :not_found # In specific action
end
end
def secondController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, :with => :rescue_not_found # In secondController
def method_in_second_controller
# Do something
end
protected
def rescue_not_found
@error = # Error object here
render :template => 'some_error_template', :status => :not_found
end
end
def ApplicationController
rescue_from ActiveRecord::RecordNotFound, :with => :rescue_not_found # Globally
protected
def rescue_not_found
@error = # Error object here
render :template => 'application/not_found', :status => :not_found
end
end
使用推荐人似乎无济于事,对于昨天的错误答案感到抱歉。