首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 3.2错误路由问题。错误ID与其他对象ID冲突

Rails 3.2错误路由问题。错误ID与其他对象ID冲突
EN

Stack Overflow用户
提问于 2012-03-21 04:28:36
回答 3查看 1.1K关注 0票数 3

我们刚刚将我们的应用升级到了Rails 3.2.2,现在在处理错误时遇到了路由问题。

对于每个José Valim's blog post,我们添加了以下内容:

config.exceptions_app = self.routes /Application.rb的配置

match "/404", :to => "errors#not_found" /routes.rb的配置

(和适当的控制器/视图)。

问题是我们需要ourdomain.com/id来显示id的产品类别的索引页。

因此,现在ourdomain.com/404显示了我们的404页面,它应该显示id为404的类别的类别列表页面。

我们该如何解决这个问题呢?

有没有办法让应用程序在routes对每个错误进行评估之前预先加上error_

或者,可能以某种方式将config.exceptions_app设置为引用routes文件中的名称空间?

或者,我是否可以创建第二个路径集并设置config.exceptions_app = self.second_set_of_routes

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-21 05:30:13

到目前为止,我找到了一个解决方案:

代码语言:javascript
运行
复制
# application_controller.rb

def rescue_404
  rescue_action_in_public CustomNotFoundError.new
end

def rescue_action_in_public(exception)
  case exception
    when CustomNotFoundError, ::ActionController::UnknownAction then
      #render_with_layout "shared/error404", 404, "standard"
      render template: "shared/error404", layout: "standard", status: "404"
    else
      @message = exception
      render template: "shared/error", layout: "standard", status: "500"
  end
end

def local_request?
  return false
end

rescue_action_in_public是Rails调用来处理大多数错误的方法。

local_request?该方法告诉Rails,如果它是本地请求,就停止执行

代码语言:javascript
运行
复制
# config/routes.rb
match '*path', controller: 'application', action: 'rescue_404' \
  unless ::ActionController::Base.consider_all_requests_local

它只是说它找不到任何其他的路由来处理请求(即*path),它应该调用应用程序控制器上的rescue_404操作(上面的第一个方法)。

编辑

这个版本对我来说效果很好!尝试添加到application.rb

代码语言:javascript
运行
复制
# 404 catch all route
config.after_initialize do |app|
  app.routes.append{ match '*a', to: 'application#render_not_found' } \
    unless config.consider_all_requests_local
end

请参阅:https://github.com/rails/rails/issues/671#issuecomment-1780159

票数 1
EN

Stack Overflow用户

发布于 2012-09-20 23:48:47

我们也遇到了同样的问题--根级别的资源的错误代码与ids冲突(例如,ourdomain.com/:resource_idourdomain.com/404之间的冲突)。

我们修改了JoséValim的解决方案,添加了一个仅在处理异常时适用的路由约束:

代码语言:javascript
运行
复制
# Are we handling an exception?
class ExceptionAppConstraint
  def self.matches?(request)
    request.env["action_dispatch.exception"].present?
  end
end

MyApp::Application.routes.draw do
  # These routes are only considered when there is an exception
  constraints(ExceptionAppConstraint) do
    match "/404", :to => "errors#not_found"
    match "/500", :to => "errors#internal_server_error"

    # Any other status code
    match '*a', :to => "errors#unknown"
  end
  ...
  # other routes, including 'match "/:resource_id"'
end

(我们昨天晚上才偶然发现了这个解决方案,所以它没有太多的老化时间。我们使用的是Rails 3.2.8)

票数 4
EN

Stack Overflow用户

发布于 2012-03-21 05:28:15

此路由似乎是在show_exceptions方法(see source)中硬编码的

对不起,除了将上面源代码中的第45行改为:

代码语言:javascript
运行
复制
env["PATH_INFO"] = "/error_#{status}"

(不用说,根本没有解决方案)。

不妨问问:如果你觉得实现自己的错误控制器这么简单很好,并且非常想拥有它,那么如果你的路由是yourdomain.com/RESTful/:id,那它不是更"RESTful“吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9794406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档