gpt4 book ai didi

ruby-on-rails - 未找到 Rails 路线但显示在 rake route

转载 作者:行者123 更新时间:2023-12-05 03:15:43 26 4
gpt4 key购买 nike

我得到以下信息:

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]

请注意,我有一个评论 Controller ,并且来自控制台的路由也不起作用:

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>

有什么建议吗?

最佳答案

您的 :id 约束当前不会匹配您想要的数字字符串。

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

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

关于ruby-on-rails - 未找到 Rails 路线但显示在 rake route ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13574644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com