我正在尝试创建一个名为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>'
我尝试过更换资源&#39;与资源&#39;以防万一不起作用;没有骰子。这里有什么问题?
答案 0 :(得分:1)
recogn_path默认为&#34; GET&#34;对于HTTP方法。您还没有使用GET方法定义快照的任何路由。尝试在传入的环境中指定HTTP方法:
r.recognize_path "/projects/1234/snapshots", method: "POST"