未找到Rails路由,但在rake路由中显示

时间:2012-11-26 23:00:39

标签: ruby-on-rails routes rake

我得到以下内容:

Started DELETE "/comments/13115742" for 127.0.0.1 at 2012-11-26 14:37:32 -0800

ActionController::RoutingError (No route matches [DELETE] "/comments/13115742"):

但'rake routes'清楚地显示了这条路线:

$ rake routes
comments POST   /comments(.:format)                comments#create {:id=>/0-9+/, :format=>/json|html/}
 comment GET    /comments/:id(.:format)            comments#show {:id=>/0-9+/, :format=>/json|html/}
         PUT    /comments/:id(.:format)            comments#update {:id=>/0-9+/, :format=>/json|html/}
         DELETE /comments/:id(.:format)            comments#destroy {:id=>/0-9+/, :format=>/json|html/}

而routes.rb明确定义了路线:

resources :comments, :constraints => {:id => /0-9+/, :format => /json|html/}, :except => [:new, :index, :edit]

请注意,我有一个注释控制器,并且从控制台路由也不起作用:

Loading development environment (Rails 3.2.8)
irb(main):001:0> r = Rails.application.routes
=> #<ActionDispatch::Routing::RouteSet:0x007f9ae17b66e0>
irb(main):002:0> r.recognize_path "/comments/3"
ActionController::RoutingError: No route matches "/comments/3"
    from /Users/nick/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:633:in `recognize_path'
    from (irb):2
    from /Users/nick/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /Users/nick/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /Users/nick/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):003:0> CommentsController.new()
=> #<CommentsController:0x007f9ae3f9d9b0 @_routes=nil, @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

您的:id约束当前与您打算使用的数字字符串不匹配。

请尝试/0-9+//[0-9]+/

,而不是/\d+/

您所拥有的内容将匹配/conversations/0-9/conversations/0-9999999

等内容