【问题标题】:after_sign_in_path_for(resource) devise + facebook omniauthafter_sign_in_path_for(resource) 设计 + facebook omniauth
【发布时间】:2025-12-28 09:55:10
【问题描述】:

我的用户模型有生日,如果在使用 Facebook 登录后它是空白的,我会尝试重定向到编辑页面。我尝试覆盖资源的 after_sign_in_path 但不断收到此错误:

在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且每个操作最多调用一次。还要注意,redirect和render都不会终止action的执行,所以如果你想在redirect后退出一个action,你需要做一些类似“redirect_to(...) and return”的操作。

我的应用程序控制器:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def after_sign_in_path_for(resource)
    stored_location_for(resource) ||
    if resource.birthday.blank?
      redirect_to edit_user_path(resource)
    end
    super
  end
end

还有我的 Omniauth 控制器:

class OmniauthCallbacksController < ApplicationController
  skip_before_filter :authenticate_user!

  def provides_callback_for
    user = User.from_omniauth(env["omniauth.auth"], current_user)
    if user.persisted?
      flash[:notice] = "You have signed in!"
    sign_in_and_redirect(user)
    else
      session['devise.user_attributed'] = user.attributes
      redirect_to new_user_registration_url
    end
  end

  def failure
    flash[:notice] = "Something went wrong!"
    redirect_to root_path
  end

  alias_method :facebook, :provides_callback_for
end

【问题讨论】:

    标签: ruby-on-rails facebook devise omniauth


    【解决方案1】:

    这应该可以解决问题:

    def after_sign_in_path_for(resource)
        if resource.birthday.blank?
          edit_user_registration_url
        else
          super
        end
    end
    

    【讨论】: