2

我得到以下信息:

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>

有什么建议么?

4

1 回答 1

4

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

而不是/0-9+/,请尝试/[0-9]+//\d+/

你所拥有的将匹配类似/conversations/0-9/conversations/0-9999999

于 2012-11-26T23:05:27.243 回答