ruby-on-rails - 未定义的方法skip_confirmation! - 设计,omniauth

标签 ruby-on-rails devise omniauth

尝试在 Heroku 上托管的应用程序上使用 devise、omniauth(包括 facebook-omniauth)设置 facebook 身份验证。
调用 facebook API 有效,但我无法在回调后跳过确认步骤。

我遵循了关于omniauth 的github 教程:https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

并阅读并尝试实现这一点:
Devise skip_confirmation! not working

但是我的heroku日志中不断出现以下错误:

NoMethodError (undefined method `skip_confirmation!')

这是我的 devise.rb 的外观:
config.omniauth :facebook, "API_KEY", "API_SECRET"    

{:strategy_class => OmniAuth::Strategies::Facebook,
:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}

这是我的 omniauth_callbacks_controller.rb :
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
   # You need to implement the method below in your model (e.g. app/models/user.rb)
   @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

   if @user.persisted?
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
    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 

这是我的 user.rb 模型:
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
 user = User.where(:provider => auth.provider, :uid => auth.uid).first
 unless user
       user = User.new(name:auth.extra.raw_info.name,
                    provider:auth.provider,
                    uid:auth.uid,
                    email:auth.info.email,
                    password:Devise.friendly_token[0,20],
                    )
       user.skip_confirmation!
       user.save
  end
  user
end

谢谢你的帮助 !

最佳答案

因此,如果我是对的(不是 100% 安全),您需要声明您的模型具有可确认模块,添加可确认模块:

   devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable

并确保您的用户表上有可确认模块的字段,您应该有字段confirmation_token 和confirmed_at

如果您没有这些字段,请查看 answer如何添加它们。

关于ruby-on-rails - 未定义的方法skip_confirmation! - 设计,omniauth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13722278/

相关文章:

ruby-on-rails - 从 gem 确定 rails 环境

ruby-on-rails - 仅当用户在 root_path 上时才在登录后重定向用户

ruby-on-rails - 当我尝试使用 Omniauth + Google Apps 时,为什么 heroku 超时?

ruby-on-rails - 使用关联模型重命名图像 - Paperclip

css - 检测 css 文件和 ruby​​ on rails 中的语法错误。无效的 CSS

ruby-on-rails - 事件管理安装错误

ruby-on-rails - 如何使用我的刷新 token 刷新我的 google_oauth2 访问 token ?

ruby-on-rails - Railscasts #241 NoMethodError 未定义方法 `[]' 为 nil :NilClass

html - HTML 标题 =""中的 Ruby 标签

ruby-on-rails - 如何使用 rspec 测试设计 View