更改默认路由Devise Rails

时间:2017-10-15 14:26:13

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 devise

我有routes.rb

  devise_for :users, :path => '', path_names: 
                          { sign_in: "login", sign_out: "logout", sign_up: "registration"}

我更改了sign_insign_up路由,如果您转到sign_up,则会收到404错误,而/registration会生效。我想要的是以相同的方式更改和添加其他路线,如忘记密码。 如果我输入控制台rake routes,我会看到这个忘记密码:

  

new_user_password GET /password/new(.:format)设计/密码#new

如何以我的自定义路由可行的方式添加其他路由,而不是默认路由?

3 个答案:

答案 0 :(得分:1)

以下是会话,注册和密码控制器操作/路由的示例:

devise_for :users, skip: [:sessions, :registrations, :passwords]

devise_scope :user do
  # sessions
  get    'login',  to: 'devise/sessions#new',     as: :new_user_session
  post   'login',  to: 'devise/sessions#create',  as: :user_session
  delete 'logout', to: 'devise/sessions#destroy', as: :destroy_user_session
  # registrations
  put    '/account',  to: 'devise/registrations#update'
  delete '/account',  to: 'devise/registrations#destroy'
  post   '/account',  to: 'devise/registrations#create'
  get    '/register', to: 'devise/registrations#new',    as: :new_user_registration
  get    '/account',  to: 'devise/registrations#edit',   as: :edit_user_registration
  patch  '/account',  to: 'devise/registrations#update', as: :user_registration
  get    '/account/cancel', to: 'devise/registrations#cancel', as: :cancel_user_registration# passwords 
  # passwords
  get   'new-pass',  to: 'devise/passwords#new',    as: :new_user_password
  get   'edit-pass', to: 'devise/passwords#edit',   as: :edit_user_password
  patch 'edit-pass', to: 'devise/passwords#update', as: :user_password
  post  'new-pass',  to: 'devise/passwords#create', as: :user_password
end

this wiki

中的第4个代码示例块所示

答案 1 :(得分:1)

您需要跳过passwords并根据需要重建其路线,

devise_for :users, skip: [:passwords]
devise_scope :user do
  match '/forgotten-password' => 'devise/passwords#create', as: :user_password, via: [:post]
  match '/forgotten-password' => 'devise/passwords#update', via: [:put, :patch]
  get 'forgotten-password', to: 'devise/passwords#new', as: :new_user_password

您的自定义路线将有效。它会跳过该模块中的所有路径。

  

注意:您需要根据需要覆盖该模块的所有剩余路由。

答案 2 :(得分:0)

请务必查看ActionDispatch :: Routing :: Mapper#devise_for文档here

你可以简单地做这样的事情 -

library(texreg)
fit_m1 <- lm(mpg ~ wt, data=mtcars)
fit_m2 <- lm(mpg ~ wt + cyl, data=mtcars)
screenreg(list(fit_m1, fit_m2))
htmlreg(list(fit_m1, fit_m2))