在Rails中将所有页面查询重定向到主页

3

我有一个简单的 Rails 应用程序作为网站过渡到新服务器的闪屏页。由于这是一个已建立的网站,因此我看到用户请求命中了 Rails 应用程序中不存在的页面。

如何将所有未知请求重定向到主页而不是抛出路由错误?

2个回答

5

我刚刚使用了路由通配符来实现这个功能:

map.connect "/*other", :controller => "pages", :action => "index"

请注意,这个路由应该放在routes.rb的末尾,以便在此之前匹配所有其他路由。

快速、完美、优雅地解决了我的问题。感谢你的帮助! - Dean Putney

0
你可以使用以下方法在一个常见的地方处理错误。将这段代码放在你的应用控制器中。
  def rescue_404
    @message = "页面未找到"
    render :template => "shared/error", :layout => "standard", :status => "404"
  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

根据需要进行修改,你也可以进行重定向等操作。

希望对你有帮助。


如果只有几个人去的地方,这可能更有用,但不幸的是我无法预测所有人来自哪些链接。感谢您的帮助。不过,您可能会发现这里发布的基于路由的解决方案也很有趣! - Dean Putney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接