我有一个在api
中命名空间的资源:
namespace :api, defaults: { format: :json } do
resources :thing
end
(结果:/api/thing/:id
)
我想在非资源 id 变量的 url 中有另一个变量:
/api/non_resource/:non_resource_id/thing/:id
如何将 :non_resource_id
变量(以及 url 的关联non_resource/
部分)添加到命名空间?
您可以使用
scope
:
namespace :api, defaults: { format: :json } do
scope '/non_resource/:non_resource_id' do
resources :thing
# other resources
end
end
事实证明,答案是为命名空间内的资源添加自定义路径前缀,而不是命名空间本身:
namespace :api, defaults: { format: :json } do
resources :thing, path: '/non_resource/:non_resource_id/things'
end
这会将该资源 url 的所有匹配项都加上字符串(包括末尾的things
,因为这会覆盖仅/things
的默认值),并允许通过thing
控制器中的params
变量访问:non_resource_id
变量。