如何在devise插件Rails中更改登录和注册URL [英] How to change the login and signup urls in devise plugin Rails

查看:79
本文介绍了如何在devise插件Rails中更改登录和注册URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新的Rails应用程序中使用devise插件。我的问题是devise插件具有用于登录和注册的默认根源

I am using devise plugin in my new Rails App. My issue is devise plugin has default roots for login and signup

/users/sign_in
/users/sign_up

我需要将其更改为

/login 
/signup

为此,我使用了以下命令路由

For this I used the following routing

 devise_for :users do
   get "login", :to => "devise/sessions#new"
   get "signup", :to => "devise/registrations#new"
 end

为此,我需要指定 login_path和 signup_path在我的视图中到处都是new_user_session_path和new_user_registration_path

With this I need to specify 'login_path' and 'signup_path' everywhere in my views where new_user_session_path and new_user_registration_path comes

我想要的是将 / login和 / signup映射到new_user_session_path和new_user_registration_path。

What I want is a configuration in routes which maps '/login' and '/signup' to new_user_session_path and new_user_registration_path.

我看到了一条帖子,其中使用以下所示的路由将/ users / sign_in和/ users / sign_up路由到/ sign_in和/ sign_up。

I have seen a post which route /users/sign_in and /users/sign_up to /sign_in and /sign_up using the below shown routing.

  devise_for :user, :as => ''

我需要这样的路由技术,将/ users / sign_in和/ users / sign_up路由到/ login和/ signup。

I need some routing technique like this which routes /users/sign_in and /users/sign_up to /login and /signup.

有人可以帮我吗?

更新:我已经更改了路由到

UPDATE: I have changed my routes.rb file to

  devise_for :users,
         :controllers => { :sessions => 'devise/sessions'},
         :skip => [:sessions] do
       get '/login' => "devise/sessions#new", :as => :new_user_session
       post '/login' => 'devise/sessions#create', :as => :user_session
       get '/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
       get '/signup' => 'devise/registrations#new', :as => :new_user_registration
  end

但是当我在我的视图中仍然使用link_to'new_user_registration'时,它不是在浏览器中显示为'/ signup'

But still when I use link_to 'new_user_registration' in my views its not showing as '/signup' in the browser

推荐答案

这里的选项比您要求的要多,但很明显:

Here are a little bit more options than you asked but it's clear:

  devise_for :users,
             :controllers => { :registrations => "users/registrations",
                               :confirmations => "users/confirmations",
                               :sessions => 'devise/sessions'},
             :skip => [:sessions] do
    get '/signin'   => "devise/sessions#new",       :as => :new_user_session
    post '/signin'  => 'devise/sessions#create',    :as => :user_session
    get '/signout'  => 'devise/sessions#destroy',   :as => :destroy_user_session
    get "/signup"   => "users/registrations#new",   :as => :new_user_registration
  end

甚至还有$ :registrations => ; 用户/注册 ,我们还可以自定义重定向:

Even more, with :registrations => "users/registrations" we can additionally customize redirects:

class Users::RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    welcome_path # it's not a home path
  end

  def after_update_path_for(resource)
    edit_user_registration_path
  end
end

Devise有一个很好的< a href = https://github.com/plataformatec/devise/wiki rel = noreferrer> Wiki 。

Devise has a good wiki.

这篇关于如何在devise插件Rails中更改登录和注册URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆