首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使Rails路由更美观

使Rails路由更美观
EN

Stack Overflow用户
提问于 2013-06-25 13:34:26
回答 1查看 140关注 0票数 0

我的Rails routes.rb中包含以下内容:

代码语言:javascript
复制
  resource :sign_up, only: [:new, :create]
  resources :users
  get 'users/activate/:token' => 'users#activate', as: 'activate_user'

这为我提供了以下路由:

代码语言:javascript
复制
       Prefix Verb   URI Pattern                      Controller#Action
      sign_up POST   /sign_up(.:format)               sign_ups#create
  new_sign_up GET    /sign_up/new(.:format)           sign_ups#new
        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
activate_user GET    /users/activate/:token(.:format) users#activate

我想摆脱get 'users/activate/:token' ...路由,转而使用嵌套或作用域,尽管我不能弄清楚。有没有办法做到这一点?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-06-25 13:59:33

您可以为用户设置采集路径:

代码语言:javascript
复制
resources :users do
  collection do
    get 'activate/:token', :action => :activate, :as => :activate
  end
end

它会给你这样的路由:

代码语言:javascript
复制
        Prefix Verb   URI Pattern                      Controller#Action
activate_users GET    /users/activate/:token(.:format) users#activate
         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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17289861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档