0

我的最后一行routes.rb是这样的:

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

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

问题是我正在尝试使用 livereload,机架中间件版本,正在发生的事情是它正在向 /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请求,或者我如何以另一种方式处理呢?

4

1 回答 1

1

You can use a custom constraint on your route to tell to ignore any special route, since its a simple rule you can do it inline, you can check for req.env["PATH_INFO"] or you you can also check for req.params[:id]

example 1:

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

example 2:

resources :tags, path: "", except: [:index, :new, :create], constraints: lambda{ |req| req.params[:id] != '/livereload' && req.params[:id] =~ /.*/ }
于 2013-03-31T08:44:14.187 回答