如何忽略特定路径?

时间:2013-03-31 00:50:59

标签: ruby-on-rails ruby-on-rails-3 routing livereload

我的routes.rb中的最后一行是:

resources :tags, path: "", except: [:index, :new, :create], constraints: { :id => /.*/ }

基本上处理所有/标记名。

问题是我正在尝试使用livereload,rack middleware version正在发生的事情是它正在向/ livereload发送ping。

但是,上面的路由拦截它并将其发送到我的TagsController ....所以我的日志文件如下所示:

Started GET "/livereload" for 192.168.1.1 at 2013-03-30 19:49:13 -0500
Processing by TagsController#show as HTML
  Parameters: {"id"=>"livereload"}
  Tag Load (3.3ms)  SELECT "tags".* FROM "tags" WHERE "tags"."name" = 'livereload' LIMIT 1
  Tag Load (2.0ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1  [["id", "livereload"]]
Completed 404 Not Found in 9ms

ActiveRecord::RecordNotFound (Couldn't find Tag with id=livereload):
  app/controllers/tags_controller.rb:16:in `show'

那么如何告诉该路由忽略所有/livereload请求或如何处理另一种方式呢?

1 个答案:

答案 0 :(得分:1)

您可以在路线上使用自定义约束来告知忽略任何特殊路线,因为它可以内联的简单规则,您可以检查 req.env [“PATH_INFO”] 或者你也可以检查 req.params [:id]

示例1:

resources :tags, path: "", except: [:index, :new, :create], constraints: lambda{ |req| req.env['PATH_INFO'] != '/livereload' && req.params[:id] =~ /.*/ }

示例2:

resources :tags, path: "", except: [:index, :new, :create], constraints: lambda{ |req| req.params[:id] != '/livereload' && req.params[:id] =~ /.*/ }