Ruby on rails “如何选择路线”;嵌套的;使用“名称空间”或“作用域:模块”/“作用域:路径”的路径?

Ruby on rails “如何选择路线”;嵌套的;使用“名称空间”或“作用域:模块”/“作用域:路径”的路径?,ruby-on-rails,ruby,ruby-on-rails-4,routing,routes,Ruby On Rails,Ruby,Ruby On Rails 4,Routing,Routes,我使用的是RubyonRails4.2,我希望通过使用命名空间或范围:模块/范围:路径在资源块中路由“嵌套”路径 也就是说,我有以下的路线: resources :users, :only => [:show] 相配 user_path GET /users/:id(.:format) users#show delete_user_session_path GET /users/:user_id/session/delete(.:format)

我使用的是RubyonRails4.2,我希望通过使用
命名空间
范围:模块
/
范围:路径
资源
块中路由“嵌套”路径

也就是说,我有以下的路线:

resources :users, :only => [:show]
相配

user_path    GET    /users/:id(.:format)    users#show
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
我想匹配以下路径

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
我读了这本书,我试着说

resources :users, :only => [:show] do
  scope :module => :users do
    scope :module => :sessions do 
    # scope :path => :sessions do
    # namespace :sessions do
      ...
    end
  end
end
new_user_session_path        GET       /users/session/new(.:format)       users/sessions#new
user_session_path            POST      /users/session(.:format)           users/sessions#create
但没有一次尝试成功。我该如何陈述路线


在@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
相配

user_path    GET    /users/:id(.:format)    users#show
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
操作,而无需传递
:user\u id
参数。也就是说,我想画一些

resources :users, :only => [:show] do
  scope :module => :users do
    scope :module => :sessions do 
    # scope :path => :sessions do
    # namespace :sessions do
      ...
    end
  end
end
new_user_session_path        GET       /users/session/new(.:format)       users/sessions#new
user_session_path            POST      /users/session(.:format)           users/sessions#create

我认为您将它复杂化了:您不需要使用
scope
path
来呈现嵌套资源。您只需将其嵌套:

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\u user\u
,或者您更愿意使用更标准的路由。如果你真的需要确切的路线,请告诉我


在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'