设计:尽管有我的自定义条目,但重复的路由条目

时间:2019-05-26 18:29:27

标签: ruby-on-rails devise routes

运行rake routes | grep sessions时,尽管已经进行了自己的设置,但我得到的条目重复。

这是我的输出:(并不是所有重复项都是这样)

    new_user_session GET    /users/sign_in(.:format)  devise/sessions#new
        user_session POST   /users/sign_in(.:format)  devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
               login GET    /login(.:format)          devise/sessions#new
                     POST   /login(.:format)          devise/sessions#create
              logout DELETE /logout(.:format)         devise/sessions#destroy

前三个是Devise的默认路由设置,后三个是我在config/routes.rb中添加的设置。

这是我的config/routes.rb文件中有关Devise的示例:

Rails.application.routes.draw do
  root to: 'pages#index'

  # Users
  devise_for :users, controllers: {
     # I override a few actions in my own controller so I needed this
     registrations: 'users/registrations',
  }

  # FYI: as is an alias for devise_scope
  as :user do
    # devise/sessions_controller handlers
    get    'login',           to: 'devise/sessions#new'
    post   'login',           to: 'devise/sessions#create'
    delete 'logout',          to: 'devise/sessions#destroy'

    #.... other registrations controller settings that are also getting duplicated
  end

  # .... all my other application routes
end

我想我要问的是,自从创建自己的路由以来,如何抑制来自Devise的默认路由?如果您需要更多我的rake routes输出或config/routes.rb文件,我可以提交整个文件。

编辑:

好吧,好像我需要向:path_names块添加一个devise_for :users哈希。在https://devhints.io/devise中,我可以在页面末尾看到一些路径选项。同样,完全删除了:path选项,可以得到一个与我想要的版本相匹配的未混淆版本。

这是我要做的(大部分)正确的事情:

devise_for :users, :path => '',
  :path_names => {
    :sign_in => 'sign-in',
    :sign_out => 'sign-out',
    :password => '',
    :confirmation => 'verification',
    :unlock => 'unblock',
    :registration => 'register',
    :sign_up => '' },
    # ignore this, changing this probably wont fix anything plus I need it.
    controllers: {
     registrations: 'users/registrations'
  }

它并不完全完美,所以我会给任何可以告诉我如何获得的答案:

                    root GET    /                          pages#index
        new_user_session GET    /sign-in(.:format)         devise/sessions#new
            user_session POST   /sign-in(.:format)         devise/sessions#create
    destroy_user_session DELETE /sign-out(.:format)        devise/sessions#destroy
cancel_user_registration GET    /register/cancel(.:format) users/registrations#cancel
   new_user_registration GET    /register(.:format)        users/registrations#new
  edit_user_registration GET    /register/edit(.:format)   users/registrations#edit
       user_registration PATCH  /register(.:format)        users/registrations#update
                          PUT    /register(.:format)       users/registrations#update
                          DELETE /register(.:format)       users/registrations#destroy
                          POST   /register(.:format)       users/registrations#create
                 password GET    /password(.:format)       devise/passwords#new
           reset_password GET    /reset-password(.:format) devise/passwords#edit
                          PATCH  /reset-password(.:format) devise/passwords#update
                          PUT    /reset-password(.:format) devise/passwords#update
                          POST   /password(.:format)       devise/passwords#create

 I DONT WANT THE FOLLOWING IN MY ROUTES:
       new_user_password GET    /new(.:format)             devise/passwords#new
      edit_user_password GET    /edit(.:format)            devise/passwords#edit
           user_password PATCH  /                          devise/passwords#update
                         PUT    /                          devise/passwords#update
                         POST   /                          devise/passwords#create

我可以将:password => ''更改为:password => 'reset-password',但是我得到了:

 new_user_password GET  /reset-password/new(.:format)  devise/passwords#new
edit_user_password GET  /reset-password/edit(.:format) devise/passwords#edit
                   POST /reset-password(.:format)      devise/passwords#create

这是我想要的时候:

 new_user_password GET  /password(.:format)       devise/passwords#new
edit_user_password GET  /reset-password(.:format) devise/passwords#edit
                   POST /password(.:format)       devise/passwords#create

1 个答案:

答案 0 :(得分:0)

好吧,好像我需要向:path_names块添加一个devise_for :users哈希。在https://devhints.io/devise中,我可以在页面末尾看到一些路径选项。同样,完全删除了:path选项,可以得到一个与我想要的版本相匹配的未混淆版本。

编辑:

除了我发现的(并包含在我的问题中)之外,我还发现了:skip选项。有人已经问过我一个类似的问题,对此的完美答案(告诉Devise不要生成我要覆盖的任何路由)

https://stackoverflow.com/a/46900434/43792