Rails设计将经过身份验证的用户重定向到特定路径

时间:2017-02-16 14:15:03

标签: ruby-on-rails devise routing ruby-on-rails-5 omniauth-facebook

问题

使用Devise时,user应在已登录时重定向到设计控制器edit_user_registration_path操作的registrations#edit

描述

routes.rb中,设置为root :to => 'main#welcome'。即使已经登录,用户仍然会被重定向到此页面。

非常感谢 的Fabrizio

3 个答案:

答案 0 :(得分:1)

Devise提供user_signed_in?方法。

这将是您welcome

中的main_controller.rb方法
def welcome 

    if user_signed_in?
        redirect_to edit_user_registration_path
    else
        // put other page in here
    end        
end

答案 1 :(得分:1)

也许你正在寻找的是一种在用户登录后将用户重定向到某个地方的方法。这应该是这样做的。将其放在app/controllers/application_controller.rb中,并放在要将用户带到

的路径中
  def after_sign_in_path_for(resource)
    edit_user_registration_path # or any other path needed
  end

答案 2 :(得分:0)

这可能会引起误解,但用户在sign up之后通过devise登录 在Devise::RegistrationsController#create

内部调用的方法
class Devise::RegistrationsController < DeviseController

  # POST /resource
  def create
    build_resource(sign_up_params)
    # omitted code
    resource.save
    sign_up(resource_name, resource)

哪个sign_in()用户

def sign_up(resource_name, resource)
  sign_in(resource_name, resource)
end

所以要增强的方法是

def after_sign_in_path_for(resource)
  edit_user_registration_path
end