我在名称间隔的控制器/用户目录中有一个控制器,所以它的第一行是
class User::BookingsController < ApplicationController
我已按如下方式设置路线
resources :users do
namespace :user do
resources :bookings
end
end
索引操作的路径是
user_user_bookings_path
或
/users/:user_id/user/bookings(.:format)
工作正常,但双user_user
听起来像是一个Catch 22的笑话。有更优雅的方式吗?
答案 0 :(得分:1)
您想要达到什么目标?您 命名空间位于&#34; user&#34;毕竟。您可以尝试as: nil
之类的选项,但我认为这会更有趣(user__bookings_path
)。
你能做的就是手工编写每条路线,例如:
resources :users do
post 'bookings', to: 'user/bookings#create'
end
# => user_bookings_path, POST /users/:user_id/bookings
或者如果您想保留网址
resources :users do
post '/user/bookings', to: 'user/bookings#create', as: 'bookings'
end
# => user_bookings_path, POST /users/:user_id/user/bookings
答案 1 :(得分:0)
试试这个
resources :users do
resources :bookings
end
答案 2 :(得分:0)
根据docs,您可以命名资源 通过使用命名空间块
namespace :api do
resources :users
end
这会给你这些路线:
但是,您希望将资源嵌套在另一个资源(docs)中,您可以执行此操作:
resources :users do
resources :bookings
end
哪会导致这些路线:
因为你的控制器的作用域是User
,你必须像这样设置用户范围:
resources :users do
resources :bookings, module: :user
end
这导致了这些路线:
➜ playground rake routes
Prefix Verb URI Pattern Controller#Action
user_bookings GET /users/:user_id/bookings(.:format) user/bookings#index
POST /users/:user_id/bookings(.:format) user/bookings#create
new_user_booking GET /users/:user_id/bookings/new(.:format) user/bookings#new
edit_user_booking GET /users/:user_id/bookings/:id/edit(.:format) user/bookings#edit
user_booking GET /users/:user_id/bookings/:id(.:format) user/bookings#show
PATCH /users/:user_id/bookings/:id(.:format) user/bookings#update
PUT /users/:user_id/bookings/:id(.:format) user/bookings#update
DELETE /users/:user_id/bookings/:id(.:format) user/bookings#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy