试图让用户使用Facebook登录,但我得到了
路由错误:未初始化的常量Users :: OmiauthCallbackController错误
类Users :: OmniauthCallbacksController<设计:: 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” root“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
答案 0 :(得分:1)
在我的路线文件中拼错了一些内容。应该说omniauth_callbacks
Fbapp::Application.routes.draw do
devise_for :users, controllers: {omniauth_callbacks: 'users/omniauth_callbacks'}
resources :posts
get "pages/home"
root "pages#home"