2015-02-09 69 views
3

我正在使用Ruby on Rails 4.2,並且我想通過在resource的塊內使用namespacescope :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 

,但我仍然需要映射newcreate行動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 

回答

0

我認爲你是它過於複雜:你不需要使用scopepath渲染嵌套的資源。你只需將其嵌套:

resources :users, :only => [:show] do 
    resources :sessions 
end 

將呈現以下途徑:

user_sessions  GET /users/:user_id/sessions(.:format)   sessions#index 
        POST /users/:user_id/sessions(.:format)   sessions#create 
new_user_session GET /users/:user_id/sessions/new(.:format)  sessions#new 
edit_user_session GET /users/:user_id/sessions/:id/edit(.:format) sessions#edit 
user_session  GET /users/:user_id/sessions/:id(.:format)  sessions#show 
        PATCH /users/:user_id/sessions/:id(.:format)  sessions#update 
        PUT /users/:user_id/sessions/:id(.:format)  sessions#update 
        DELETE /users/:user_id/sessions/:id(.:format)  sessions#destroy 
user    GET /users/:id(.:format)       users#show 

這些都不是你提到你需要的路線,但希望我你重新考慮,如果你真的需要他們這樣的,命名空間控制器和自定義名稱,如delete_user_,或者您更喜歡更多標準。如果你確實需要這些確切路線,請告訴我。


UPDATE後OP的更新

爲了讓兩條路線失蹤,你需要他們走出休息資源。我只是直接寫這樣的:

resources :users, :only => [:show] do 
    scope :module => :users do 
    resource :session, :only => [:show, :destroy] do 
     get :delete, :on => :collection, :to => 'sessions#delete' 
    end 
    end 
end 

get 'users/session/new', to: 'users/sessions#new', as: :new_user_session 
post 'users/session', to: 'users/sessions#create' 
+0

我更新了你的答案後的問題。 – user502052 2015-02-09 08:54:00