简体   繁体   中英

Rails route not found but shows in rake routes

I am getting the following:

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"):

But 'rake routes' clearly shows this route:

$ 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/}

And routes.rb clearly has the route defined:

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

Note that I have a comments controller, and routing from the console doesn't work either:

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>

Any suggestions?

Your :id constraint currently is not going to match the numeric string you intend for it to.

Instead of /0-9+/ , please try /[0-9]+/ or /\\d+/

What you have will match something like /conversations/0-9 or /conversations/0-9999999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM