ruby-on-rails - 如何使用 "nested"或 `namespace`/`scope :module` 路由 `scope :path` 路径?

标签 ruby-on-rails ruby ruby-on-rails-4 routing routes

我正在使用 Ruby on Rails 4.2,我想使用 namespace 路由“嵌套”路径或 scope :module/scope :pathresource 的 block 内.

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

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无需通过 :user_id 的操作范围。也就是说,我想映射类似

new_user_session_path        GET       /users/session/new(.:format)       users/sessions#new
user_session_path            POST      /users/session(.:format)           users/sessions#create

最佳答案

我认为您过于复杂了:您不需要使用 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

那些不是你提到的你需要的路线,但我希望你重新考虑你是否真的需要它们,使用命名空间 Controller 和自定义名称,如 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'

关于ruby-on-rails - 如何使用 "nested"或 `namespace`/`scope :module` 路由 `scope :path` 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28401887/

相关文章:

ruby-on-rails - 与 unicorn/thin/puma/etc 相比 passenger 5 的性能

ruby - 未能要求本地 gem

ruby-on-rails - session 在 Rails 中是如何工作的

ruby-on-rails - Rails 4 形式 : checkbox not saving value to database

ruby-on-rails - Rails 4 - has_one - 无法访问相关模型的属性

ruby-on-rails - Rails 3 路线是否还有单一选项?

ruby-on-rails - 如何从单个事件记录对象获取列值

Ruby:如何将条件插入字符串连接

javascript - 禁用 JavaScript 的 RoR

ruby-on-rails - rails has_many :through has_many :through