在 rails 应用程序中使用 Devise gem 时,Users::OmniauthCallbacksController#facebook 出现 ActionController::UnknownFormat 错误(缺少模板)?

ActionController::UnknownFormat error (missing template) in Users::OmniauthCallbacksController#facebook when using Devise gem in a rails application?

我正在尝试完成此 facebook omniauth 集成并 运行 进入以下错误。我不确定是什么导致它发生。它仅在我第二次尝试使用 facebook 身份验证登录时发生。第一次(在用户创建时)它完美运行。但是,如果我注销并重新登录,则会抛出此错误。

这是错误

ActionController::UnknownFormat in Users::OmniauthCallbacksController#facebook

Users::OmniauthCallbacksController#facebook is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

User.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  has_many :services
  has_paper_trail
  scope :referred, -> { where.not(:referral_id => nil) }

end

omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  before_action :send_referral_event_email, only: [:create]


  def facebook
    service = Service.where(provider: auth.provider, uid: auth.uid).first

    if service.present?
      user = service.user
      service.update(
        expires_at: Time.at(auth.credentials.expires_at),
        access_token: auth.credentials.token,
      )
    else
      user = User.create(
         email: auth.info.email,
         name: auth.info.name,
         password: Devise.friendly_token[0,20]
      )
      user.services.create(
         provider: auth.provider,
         uid: auth.uid,
         expires_at: Time.at(auth.credentials.expires_at),
         access_token: auth.credentials.token,
      )
      if session[:cte_id]
        UserMailer.delay.send_referred_message(user)
        sign_in_and_redirect user, event: :authentication
        set_flash_message :notice, :success, kind: "Facebook"
      else
        ActionWins.create_advocate(user.email)
        UserMailer.delay.advocate_registration_email(user)
        sign_in_and_redirect user, event: :authentication
        set_flash_message :notice, :success, kind: "Facebook"
      end
    end

  end

  def auth
    p request.env['omniauth.auth']
  end

  def after_sign_in_path_for(resource)
    if session[:cte_id]
      static_thankyou_path
    else
      root_url
    end
  end

  def send_referral_event_email
    if params[:cte_id]
      UserMailer.delay.send_referred_message(@user)
    end
  end
end

我正在使用 GoRails.com FB omniauth 教程来构建它。

这是来自数据库的用户模型

 create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "",    null: false
    t.string   "encrypted_password",     default: "",    null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                             null: false
    t.datetime "updated_at",                             null: false
    t.string   "referral_id"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "phone_number"
    t.boolean  "redeemed",               default: false
    t.string   "dealership"
    t.integer  "points",                 default: 0
    t.boolean  "referral_completed",     default: false
    t.string   "name"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

感谢您的帮助!

问题是如果用户已经存在(即第二次),则不会调用将用户重定向到 root 或其他地方。

您有一个 if-else 块,用于检查用户是否是第一次访问您的应用程序。如果用户是第一次访问,那么您正在创建用户并使用

重定向用户
sign_in_and_redirect user, event: :authentication

但是,如果用户已经存在(即 if 块),则没有重定向调用。因此,默认情况下,Rails 会尝试查找不存在的视图模板 facebook.html.erb。因此,错误

Users::OmniauthCallbacksController#facebook is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

您可以在 if 块的末尾添加重定向调用

 if service.present?
      user = service.user
      service.update(
        expires_at: Time.at(auth.credentials.expires_at),
        access_token: auth.credentials.token,
      )
      #add the following line
      sign_in_and_redirect user, event: :authentication
 else
      ....