【发布时间】:2014-02-01 19:23:13
【问题描述】:
试图让用户使用 facebook 登录,但我明白了
路由错误:未初始化常量Users::OmiauthCallbackController错误
类用户::OmniauthCallbacksController
def facebook
@user = User.find_or_create_from_auth_hash auth_hash
if @user.persisted?
sign_in_and_redirect @user
else
session["devise.user_attributes"] = @user.attributes
redirect_to new_user_registration_url, notice: "Oops, something went wrong"
end
end
private
def auth_hash request.env['omniauth.auth'] 结尾 结束
路由文件
Fbapp::Application.routes.draw 做 devise_for :用户,控制器:{omniauth_callbacks:'users/omiauth_callback'} 资源:帖子
获取“页面/主页” 根“pages#home”
user rb file
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.find_or_create_from_auth_hash(auth_hash)
find_by_auth_hash(auth_hash) || create_from_auth_hash(auth_hash)
end
def self.find_by_auth_hash(auth_hash)
where(
provider: auth_hash.provider,
uid: auth_hash.uid
).first
end
def self.create_from_auth_hash(auth_hash)
create(
provider: auth_hash.provider,
uid: auth_hash.uid,
email: auth_hash.info.email,
name: auth_hash.info.name,
oauth_token: auth_hash.credentials.token,
oauth_expires_at: Time.at(auth_hash.credentials.expires_at)
)
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
结束
def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
end
def get_profile_info
self.facebook.get_object("me")
end
def get_location
h = get_profile_info["location"]
h["name"]
end
def get_books
self.facebook.get_connection("me", "books")
end
def get_profile_picture
self.facebook.get_picture(uid)
end
end
【问题讨论】:
-
你能把剩下的课贴上去吗
-
你的 routes.rb 文件中有什么?例如,我希望类似 'devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }'
-
尼尔他的意思是编辑您的帖子并将其粘贴到那里,并在其中正确格式化代码......而不是在 cmets 中。
-
如前所述添加代码
标签: ruby-on-rails facebook devise