没有命名空间前缀的命名路由
Named route without the namespace prefix
我们定义了一个到 resource_owner_info
控制器的路由,如下所示:
namespace :api, default: { format: :json } do
namespace :v1 do
get '/me', to: 'resource_owner_info#me', as: 'resource_owner_info'
# other /api/v1 routes follow
...
end
end
这会生成命名路由 api_v1_resource_owner_info
。我更愿意从这些路由中删除 api_v1_
前缀,但端点仍然是 /api/v1/...
等。可以在 Rails 中完成吗?
很确定您必须在命名空间之外执行此操作:
get 'api/v1/me', to: 'api/v1/resource_owner_info#me', as: 'resource_owner_info', defaults: { format: :json }
如果你有很多这样的路线:
scope path: :api, module: :api, defaults: { format: :json } do
scope path: :v1, module: :v1 do
get 'me', to: 'resource_owner_info#me', as: 'resource_owner_info'
# ...
end
end
两者都会生成这个
Prefix Verb URI Pattern Controller#Action
resource_owner_info GET /api/v1/me(.:format) api/v1/resource_owner_info#me {:format=>:json}
https://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html
我们定义了一个到 resource_owner_info
控制器的路由,如下所示:
namespace :api, default: { format: :json } do
namespace :v1 do
get '/me', to: 'resource_owner_info#me', as: 'resource_owner_info'
# other /api/v1 routes follow
...
end
end
这会生成命名路由 api_v1_resource_owner_info
。我更愿意从这些路由中删除 api_v1_
前缀,但端点仍然是 /api/v1/...
等。可以在 Rails 中完成吗?
很确定您必须在命名空间之外执行此操作:
get 'api/v1/me', to: 'api/v1/resource_owner_info#me', as: 'resource_owner_info', defaults: { format: :json }
如果你有很多这样的路线:
scope path: :api, module: :api, defaults: { format: :json } do
scope path: :v1, module: :v1 do
get 'me', to: 'resource_owner_info#me', as: 'resource_owner_info'
# ...
end
end
两者都会生成这个
Prefix Verb URI Pattern Controller#Action
resource_owner_info GET /api/v1/me(.:format) api/v1/resource_owner_info#me {:format=>:json}
https://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html