重定向用户注册

问题描述:

使用 before_action:authenticate_user!来检查用户是否登录,但它会将用户发送到登录而不是注册

Using before_action :authenticate_user! to check if user logged in. But it sends users to login instead of signup.

尝试将用户指向注册的不同方法而不是登录,但是在成功注册后不会将用户重新发送回原始页面。

Tried different ways of directing user to signup instead of login, but they do not send the user back to the original page after successful signup.

如何发送用户注册并将用户引导回原始页面?

How can I send a user to signup and direct a user back to the original page afterwards?

尝试:

before_filter :auth_user

def auth_user
  redirect_to new_user_registration_url unless user_signed_in?
end



路线文件



Routes File

Rails.application.routes.draw do
  devise_for :installs
  resources :orders
  resources :products
  devise_for :users

  get 'dashboard' => 'pages#dashboard'
  get 'contact' => 'pages#contact'
  get 'cart' => 'carts#index'

  root 'pages#home'


您正在寻找的是 referer
让你的before_filter工作,你可以覆盖Devise的 after_sign_up_path_for

def after_sign_up_path_for(user)
  request.referer
end






编辑



由于 request.referer 在某种程度上不起作用,我认为您可以使用基于会话的解决方案:


EDIT

Since request.referer is somehow not working, I think you could go with a session-based solution:

def auth_user
  session[:before_sign_up_path] = request.fullpath
  redirect_to new_user_registration_url unless user_signed_in?
end

def after_sign_up_path_for(user)
  session[:before_sign_up_path]
end