" resources:post,除了::new"使Rails认为路线/ posts / new将指向帖子ID“ new”, [英] "resources :post, except: :new" makes Rails think the route /posts/new would point to a post ID "new"
问题描述
我有以下路线:
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
的帖子?那是不对的?
Why does Rails think that posts/new
would point to a post with the ID new
? That's not right, is it? Does it maybe have to do with InheritedResources?
推荐答案
我相信,如果不对<$ c添加约束,那可能与InheritedResources有关吗? $ c> show 路线表示它将仅接受数字,您在帖子
之后放置的所有内容均映射为 id
转到该路线。
I believe that if you don't add a constraint to your show
route saying that it will only accept digits, everything you put after posts
are mapped to be an id
to that route.
这意味着,如果您尝试访问帖子/内容
,则可能会抛出 ActiveRecord
错误,显示找不到带有 id = something
的 Post
。
That means that if your try to access posts/something
it would probably throw an ActiveRecord
error showing that it couldn't find the Post
with id=something
.
要添加约束,请添加 constraints
,例如:
To add a constraint, add constraints
like this:
resources :success_criteria, except: :new, constraints: { id: /\d+/ }
这篇关于" resources:post,除了::new"使Rails认为路线/ posts / new将指向帖子ID“ new”,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!