【问题标题】:Redirect to "finish_sign_up" when Users subscribe with Omniauth Facebook当用户使用 Omniauth Facebook 订阅时,重定向到“finish_sign_up”
【发布时间】:2016-03-19 21:03:13
【问题描述】:

我使用 gem 'Omniauth' 安装了 Facebook 注册,现在用户可以使用它进行连接。我在我的网站中使用了 Devise。

我想知道在用户连接到 Facebook 后如何重定向路径(例如完成注册),因为他们必须填写包含特定信息的表单。

我是否在现有控制器文件中添加新条件?我创建了一个名称为omniauth_callbacks_controller.rb

代码是:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    user = User.find_for_facebook_oauth(request.env['omniauth.auth'])

    if user.persisted?
      sign_in_and_redirect user, event: :authentication
      set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
    else
      session['devise.facebook_data'] = request.env['omniauth.auth']
      redirect_to new_user_registration_url
    end
  end
end

我尝试这样做

if user.persisted?
      sign_in_and_redirect user, event: :authentication
      set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
user.thing_you_want_to_check.blank?
     redirect_to finish_sign_up_path

    else
      session['devise.facebook_data'] = request.env['omniauth.auth']
      redirect_to new_user_registration_url
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails facebook devise


    【解决方案1】:

    在会话控制器创建操作中添加一个条件来检查字段是否已填写:

    def create
      user = User.from_omniauth(env["omniauth.auth"])
      session[:user_id] = user.id
      if user.thing_you_want_to_check.blank?
         redirect_to finish_sign_up_path
      else
        redirect_to root_url, notice: "Signed In!"
      end
    end
    

    使用您提供的更新代码进行编辑:

    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    

    定义脸书 用户 = User.find_for_facebook_oauth(request.env['omniauth.auth'])

    if user.persisted? && user.some_field.blank?
       redirect_to # put the path you want them to go to here
    elsif user.persisted?
      sign_in_and_redirect user, event: :authentication
      set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
    else
      session['devise.facebook_data'] = request.env['omniauth.auth']
      redirect_to new_user_registration_url
    end
    

    结束 结束

    some_field 应该是他们将在您引导他们到的路径上填写的字段。如果它已经存在,您只希望他们登录而不必走那条路。这就是为什么我们要检查以确保它是空白的。

    【讨论】:

    • 我在上面的问题上添加了更具体的信息。请你指导我。非常感谢。
    • 您可以添加一个附加条件来检查您的user.persisted? 条件中是否存在附加字段。如果它们不存在,请将它们重定向到您想要的路径。
    • 你的意思是如果user.persisted我必须添加? sign_in_and_redirect 用户,事件::authentication set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format? else session['devise.facebook_data'] = request.env['omniauth.auth'] redirect_to new_user_registration_url end end end
    • 我正在尝试,但我创建了列 I,其中包含大量信息的字段名称。我给它命名?
    • 我不明白你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2014-09-17
    • 1970-01-01
    • 2015-05-28
    • 2016-08-16
    相关资源
    最近更新 更多