3
我正在使用Ruby on Rails 4.2,並且我想通過在resource
的塊內使用namespace
或scope :module
/scope :path
來路由「嵌套」路徑。如何使用`namespace`或`scope:module` /`scope:path`路由「嵌套」路徑?
也就是說,我有以下途徑:
resources :users, :only => [:show]
匹配
user_path GET /users/:id(.:format) users#show
我想匹配以下路徑
users_sessions_path POST /users/sessions users/sessions#create
user_session_path GET /users/:id/session users/sessions#show
delete_user_session_path GET /users/:id/session/delete users/sessions#delete
user_session_path DELETE /users/:id/session users/sessions#destroy
我讀了official documentation和我試圖陳述類似於
resources :users, :only => [:show] do
scope :module => :users do
scope :module => :sessions do
# scope :path => :sessions do
# namespace :sessions do
...
end
end
end
但沒有嘗試成功。我應該如何說明路線?
更新的@dgilperez答案後
我嘗試下面的代碼
resources :users, :only => [:show] do
scope :module => :users do
resource :session, :only => [:show, :new, :create, :destroy] do
get :delete, :on => :collection, :to => 'sessions#delete'
end
end
end
匹配
delete_user_session_path GET /users/:user_id/session/delete(.:format) users/sessions#delete
new_user_session_path GET /users/:user_id/session/new(.:format) users/sessions#new
user_session_path POST /users/:user_id/session(.:format) users/sessions#create
user_session_path GET /users/:user_id/session(.:format) users/sessions#show
DELETE /users/:user_id/session(.:format) users/sessions#destroy
,但我仍然需要映射new
和create
行動w ^無需通過:user_id
參數。也就是說,我要地圖類似
new_user_session_path GET /users/session/new(.:format) users/sessions#new
user_session_path POST /users/session(.:format) users/sessions#create
我更新了你的答案後的問題。 – user502052 2015-02-09 08:54:00