Ruby on rails 应重定向到自定义路由上的公共显示/页面/Rails中未找到页面错误

Ruby on rails 应重定向到自定义路由上的公共显示/页面/Rails中未找到页面错误,ruby-on-rails,routing,custom-routes,Ruby On Rails,Routing,Custom Routes,我正在开发Rails应用程序。在我的应用程序中,如果我在地址栏/as URL中手动输入一条自定义路由,而该地址栏/as URL在config/routes.rb中不存在,它将显示以下给定的错误消息 路由错误 没有路由匹配“/clientImage/blahblah” 我希望这是重定向到一个适当的显示所有错误的路线由用户提供的有意/无意的。任何帮助都将不胜感激 Yourapp::Application.routes.draw do #Last route in routes.rb matc

我正在开发Rails应用程序。在我的应用程序中,如果我在地址栏/as URL中手动输入一条自定义路由,而该地址栏/as URL在config/routes.rb中不存在,它将显示以下给定的错误消息

路由错误

没有路由匹配“/clientImage/blahblah”

我希望这是重定向到一个适当的显示所有错误的路线由用户提供的有意/无意的。任何帮助都将不胜感激

Yourapp::Application.routes.draw do
  #Last route in routes.rb
  match '*a', :to => 'errors#routing'
end
“a”实际上是Rails 3路由全球化技术中的一个参数。例如,如果您的url为/此url不存在,则参数[:a]等于“/此url不存在”。所以,尽你所能创造性地处理这条流氓路线

应用程序内/controllers/errors\u controller.rb

 class ErrorsController < ApplicationController
      def routing
       render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
      end
    end
类错误控制器“#{Rails.root}/public/404.html”,:status=>404,:layout=>false
结束
结束
“a”实际上是Rails 3路由全球化技术中的一个参数。例如,如果您的url为/此url不存在,则参数[:a]等于“/此url不存在”。所以,尽你所能创造性地处理这条流氓路线

应用程序内/controllers/errors\u controller.rb

 class ErrorsController < ApplicationController
      def routing
       render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
      end
    end
类错误控制器“#{Rails.root}/public/404.html”,:status=>404,:layout=>false
结束
结束

当有人输入不受支持的url时,Rails将引发ActionController::RoutingError。您可以挽救此错误并呈现404未找到的html

Rails提供了一些为此目的而调用的特殊函数

class ApplicationController < ActionController::Base
   rescue_from ActionController::RoutingError, :with => :render_not_found
   rescue_from StandardError, :with => :render_server_error

   protected
     def render_not_found
        render "shared/404", :status => 404
     end

     def render_server_error
        render "shared/500", :status => 500
     end
end
class ApplicationController:未找到渲染\u
从标准错误中解救\u,:with=>:呈现\u服务器\u错误
受保护的
未找到def render_
呈现“共享/404”:状态=>404
结束
def呈现服务器错误
呈现“共享/500”,状态=>500
结束
结束

当有人输入不受支持的url时,将404.html、500.html放入app/views/shared

,Rails将引发ActionController::RoutingError。您可以挽救此错误并呈现404未找到的html

Rails提供了一些为此目的而调用的特殊函数

class ApplicationController < ActionController::Base
   rescue_from ActionController::RoutingError, :with => :render_not_found
   rescue_from StandardError, :with => :render_server_error

   protected
     def render_not_found
        render "shared/404", :status => 404
     end

     def render_server_error
        render "shared/500", :status => 500
     end
end
class ApplicationController:未找到渲染\u
从标准错误中解救\u,:with=>:呈现\u服务器\u错误
受保护的
未找到def render_
呈现“共享/404”:状态=>404
结束
def呈现服务器错误
呈现“共享/500”,状态=>500
结束
结束

将404.html、500.html放在app/views/shared中

您可能正在开发环境中工作。在生产环境中,您只需在公共目录中放置一个404.html页面来定制displayYes,I'm In development env。谢谢您提供的信息。您可能正在开发环境中工作。在生产环境中,您只需在公共目录中放置一个404.html页面来定制displayYes,I'm In development env。感谢您提供的信息。这意味着我需要为所有URL(每个控制器)提供“匹配”语句。正确的?这将是一项乏味的任务。这意味着我需要为所有URL(每个控制器)提供“匹配”语句。正确的?那将是一项乏味的任务。