2012-11-26 97 views
1

我得到以下幾點:Rails的路徑沒有找到,但顯示了在耙路線

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 
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

您的:id約束條件目前不符合您打算使用的數字字符串。

相反的/0-9+/,請嘗試/[0-9]+//\d+/

你會匹配像/conversations/0-9/conversations/0-9999999

+0

但格式是可選的什麼(:格式)?爲什麼要這麼做?但是,即使刪除該約束也不能解決該問題。 –

+0

另請注意,以下路線不能解析: '/ comments''/comments.html''/comments.json''/comments/3.html''/comments/3.json' –

+0

我已經更新了我的答案*(我實際上是在我的應用程序中測試了這個時間:))*。 – deefour

相關問題