“资源:post,除了:: new”让Rails认为路由/post/新会指向postID“新”

我有以下路线:

resources :success_criteria, except: :new 

以下规范失败:

 describe SuccessCriteriaController do describe 'routing' do it 'routes to #new' do expect(get('/success_criteria/new')).to_not be_routable end end end 

失败消息:

 Failure/Error: expect(get('/posts/new')).to_not be_routable expected {:get=>"/posts/new"} not to be routable, but it routes to {:controller=>"posts", :action=>"show", :id=>"new"} 

控制器看起来像这样:

 class SuccessCriteriaController < InheritedResources::Base load_and_authorize_resource end 

为什么Rails认为posts/new会指向ID为new的post? 那不对,是吗? 它可能与InheritedResources有关吗?

我相信,如果你没有为你的show路线添加一个约束,说它只接受数字,你在posts之后放的所有内容都被映射为该路由的id

这意味着如果你尝试访问posts/something它可能会抛出一个ActiveRecord错误,表明它无法找到id=somethingPost

要添加约束,请添加如下constraints

 resources :success_criteria, except: :new, constraints: { id: /\d+/ }