新手问题 ruby-china 如何处理输入的无效 url 路径

msl12 · 2016年04月09日 · 最后由 leiz_me 回复于 2016年04月10日 · 2970 次阅读

我看到了源码里 routes.rb 文件里有这样一句:

match '*path', via: :all, to: 'home#error_404'

我把这句也加到了我的项目里,只是我改成了"match '*path'=>'welcome#error_404', via: :all",然后我的路由是这样的:

Prefix Verb   URI Pattern                                Controller#Action
              ru_captcha        /rucaptcha                                 RuCaptcha::Engine
        new_user_session GET    /users/sign_in(.:format)                   sessions#new
            user_session POST   /users/sign_in(.:format)                   sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                  sessions#destroy
           user_password POST   /users/password(.:format)                  devise/passwords#create
       new_user_password GET    /users/password/new(.:format)              devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)             devise/passwords#edit
                         PATCH  /users/password(.:format)                  devise/passwords#update
                         PUT    /users/password(.:format)                  devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                    account#cancel
       user_registration POST   /users(.:format)                           account#create
   new_user_registration GET    /users/sign_up(.:format)                   account#new
  edit_user_registration GET    /users/edit(.:format)                      account#edit
                         PATCH  /users(.:format)                           account#update
                         PUT    /users(.:format)                           account#update
                         DELETE /users(.:format)                           account#destroy
                    root GET    /                                          welcome#index
            destroy_plan DELETE /plans/:id(.:format)                       plans#destroy
            plan_replies GET    /plans/:plan_id/replies(.:format)          replies#index
                         POST   /plans/:plan_id/replies(.:format)          replies#create
         edit_plan_reply GET    /plans/:plan_id/replies/:id/edit(.:format) replies#edit
              plan_reply PATCH  /plans/:plan_id/replies/:id(.:format)      replies#update
                         PUT    /plans/:plan_id/replies/:id(.:format)      replies#update
                         DELETE /plans/:plan_id/replies/:id(.:format)      replies#destroy
                   plans GET    /plans(.:format)                           plans#index
                         POST   /plans(.:format)                           plans#create
                new_plan GET    /plans/new(.:format)                       plans#new
               edit_plan GET    /plans/:id/edit(.:format)                  plans#edit
                    plan PATCH  /plans/:id(.:format)                       plans#update
                         PUT    /plans/:id(.:format)                       plans#update
            destroy_user DELETE /users/:id(.:format)                       users#destroy
                                /*path(.:format)                           welcome#error_404

Routes for RuCaptcha::Engine:
  root GET  /           ru_captcha/captcha#index

当我输入 url 路径为"http://localhost:3000/plans/85/edit85"这个:plan_id并不存在),发现会出现这样的错误:"时 (事实上"

Couldn't find Plan with 'id'=85

Extracted source (around line #155):
        record = s.execute([id], self, connection).first
        unless record
          raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
        end
        record
      rescue RangeError

但是我看到 ruby-china 源码里面似乎也没怎么使用 rescue 之类的来处理这种问题,于是想问问各位,怎么实现遇到这种找不到 plan 能够跳转到某某 404 页面的效果呢?

开发模式下会显示详细的错误信息的

#1 楼 @imconfused

Started GET "/plans/85/edit" for 127.0.0.1 at 2016-04-09 12:56:16 +0800
Processing by PlansController#edit as HTML
  Parameters: {"id"=>"85"}
  Plan Load (0.1ms)  SELECT  "plans".* FROM "plans" WHERE "plans"."id" = ? LIMIT 1  [["id", 85]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.5ms)

ActiveRecord::RecordNotFound (Couldn't find Plan with 'id'=85):
  activerecord (4.2.5.1) lib/active_record/core.rb:155:in `find'
  cancancan (1.13.1) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
  cancancan (1.13.1) lib/cancan/controller_resource.rb:111:in `find_resource'
  cancancan (1.13.1) lib/cancan/controller_resource.rb:63:in `load_resource_instance'
  cancancan (1.13.1) lib/cancan/controller_resource.rb:32:in `load_resource'
  cancancan (1.13.1) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
  cancancan (1.13.1) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:448:in `instance_exec'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:448:in `block in make_lambda'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:164:in `call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:164:in `block in halting'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `block in call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `each'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.5.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.5.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.5.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.5.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.5.1) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.5.1) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.5.1) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.5.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.5.1) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:815:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `catch'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.5.1) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.5.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.5.1) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.5.1) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /home/moshenglong/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /home/moshenglong/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /home/moshenglong/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.8ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (15.9ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.5ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (13.6ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
  Rendered /home/moshenglong/.rvm/gems/ruby-2.0.0-p643/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (27.1ms)

这是所有的 bash 里面所有的错误信息了,还是不知道如何解决

在你的 Plan controller 里边,处理 record not found 的情况。

我的意思是说如果在生产环境是不会有这样的出错信息的,他是直接显示 404 错误页面,但是如果在开发环境下就是目前你看到的页面

#4 楼 @imconfused 哦,原来如此,谢谢!

楼主这一行代码要放在 routes.rb 的最下面

match '*path', via: :all, to: 'home#error_404'

你搞错了这里的搜索逻辑。 match '*path', via: :all, to: 'home#error_404' 这一行的意思是,如果上面所有的 route 规则都找不到,才匹配这条规则。 但是你用来测试的 testcase 明显已经匹配到了 plan/:id 的规则,只是:id 是不存在而已。这种情况下你要针对 id 不存在进行容错处理,例如直接跳转 welcome#404。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号