mardi 12 juillet 2016

Rails route not found, but exists in rake routes

I'm trying to create a new resource called a Snapshot, to consolidate a couple of actions that probably should be a resource of their own rather than cluttering up the ProjectsController. I'm sure I've set everything up correctly, but Rails doesn't recognise the route.

Here's the relevant excerpt from my routes.rb:

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

It shows up when I run rake routes just fine:

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

But in the rails console, I don't get any joy:

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

I've tried replacing 'resources' with 'resource' just in case that doesn't work; no dice. What's wrong here?

Aucun commentaire:

Enregistrer un commentaire