Rails 路由未找到,但存在于 rake 路由中

Rails route not found, but exists in rake routes

我正在尝试创建一个名为 Snapshot 的新资源,以合并一些可能应该是它们自己的资源的操作,而不是弄乱 ProjectsController。我确定我已正确设置所有内容,但 Rails 无法识别路线。

这是我的 routes.rb 的相关摘录:

resources :projects do
  resources :snapshots, only: [:create, :update]
end

当我 运行 rake routes 就好了:

project_snapshots POST     /projects/:project_id/snapshots(.:format)                                          snapshots#create
 project_snapshot PATCH    /projects/:project_id/snapshots/:id(.:format)                                      snapshots#update
                  PUT      /projects/:project_id/snapshots/:id(.:format)                                      snapshots#update

但是在 rails 控制台中,我没有得到任何快乐:

r = Rails.application.routes
=> #<ActionDispatch::Routing::RouteSet:0x007faa06b5b730>

r.recognize_path "/projects/1234/snapshots"
ActionController::RoutingError: No route matches "/projects/1234/snapshots"
    from /Users/merus/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:840:in `recognize_path'
    from (irb):4
    from /Users/merus/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /Users/merus/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/merus/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/merus/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/merus/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

我试过用 'resource' 替换 'resources' 以防万一不起作用;没有骰子。这里有什么问题?

对于 HTTP 方法,

recognize_path 默认为 "GET"。您尚未使用 GET 方法为快照定义任何路由。尝试在您传入的环境中指定 HTTP 方法:

r.recognize_path "/projects/1234/snapshots", method: "POST"