如果已经使用signed_in用户尝试再次注册,则自定义设计重定向

jam*_*mes 3 redirect ruby-on-rails devise ruby-on-rails-4

我觉得这很容易......

我对默认的Devise控制器进行了很少的自定义,我的注册#New中的自定义旨在确保在视图中可以访问某些变量.我已经阅读了源代码,但是如果正在访问注册页面的用户已经登录,我不确定我是否看到重定向用户的行?

基本上,如果用户已经登录,如果他/她访问了注册页面,我想将他/她重定向到仪表板页面.目前,重定向将转到根页面.

我怎么能改变这个?

我的代码:

class Users::RegistrationsController < Devise::RegistrationsController
def new
  gon.zipcode_list = zipcode_list
  gon.all_invite_codes = all_invite_codes
  selected_plan_array 
  meal_type_array 

  super
end
end
Run Code Online (Sandbox Code Playgroud)

源代码:

def new
  build_resource({})
  set_minimum_password_length
  yield resource if block_given?
  respond_with self.resource
end
Run Code Online (Sandbox Code Playgroud)

路线:

devise_for :users, controllers: { registrations: "users/registrations", sessions: "users/sessions" }

root "staticpages#home" 
#^ the above is where a user is being redirected if s/he is already signed in and visiting the sign up page
Run Code Online (Sandbox Code Playgroud)

基本上我喜欢下面的东西

def after_existing_sign_in_path_for(resource)
  dashboard_path
end
Run Code Online (Sandbox Code Playgroud)

Jam*_*ani 5

重定向用户的源代码中的代码是filter:require_no_authentication.如果您覆盖Devise :: SessionsController以跳过该过滤器,您将能够将您的用户重定向到您选择的路径.

像这样:

class Users::SessionsController < Devise::SessionsController
  # Note that all the other actions are handled by Devise::SessionsController
  # (which is in the gem)
  skip_filter :require_no_authentication, only: :new
  def new
    if user_signed_in?
      redirect_to dashboard_path
      return
    end
    super
  end
end
Run Code Online (Sandbox Code Playgroud)