ruby-on-rails – Devise,Omniauth和Facebook集成会话错误
我是Rails的新手,正在安装我的第一个集成了Facebook登录/注册的应用程序.我关注了Ryan Bates在这里发布的RailsCast视频:
https://stackoverflow.com/a/4954443/1302333.
我按照他的教程来整合Twitter,这有用,但我遇到了Facebook的问题.当我访问http:// localhost:3000 / auth / facebook时,我收到此错误: NoMethodError in AuthenticationsController#create undefined method `serialize_into_session' for Symbol:Class 这是完整的痕迹: devise (2.1.0) lib/devise/rails/warden_compat.rb:19:in `serialize' warden (1.1.1) lib/warden/session_serializer.rb:25:in `store' warden (1.1.1) lib/warden/proxy.rb:161:in `set_user' devise (2.1.0) lib/devise/controllers/helpers.rb:124:in `sign_in' devise (2.1.0) lib/devise/controllers/helpers.rb:236:in `sign_in_and_redirect' app/controllers/authentications_controller.rb:11:in `create' actionpack (3.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (3.2.6) lib/abstract_controller/base.rb:167:in `process_action' actionpack (3.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (3.2.6) lib/abstract_controller/callbacks.rb:18:in `block in process_action' activesupport (3.2.6) lib/active_support/callbacks.rb:414:in `_run__1508312784814672110__process_action__3616665370535505566__callbacks' activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.6) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (3.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' activesupport (3.2.6) lib/active_support/notifications.rb:123:in `block in instrument' activesupport (3.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (3.2.6) lib/active_support/notifications.rb:123:in `instrument' actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:29:in `process_action' actionpack (3.2.6) lib/action_controller/metal/params_wrapper.rb:206:in `process_action' activerecord (3.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (3.2.6) lib/abstract_controller/base.rb:121:in `process' actionpack (3.2.6) lib/abstract_controller/rendering.rb:45:in `process' actionpack (3.2.6) lib/action_controller/metal.rb:203:in `dispatch' actionpack (3.2.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' actionpack (3.2.6) lib/action_controller/metal.rb:246:in `block in action' actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `call' actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `dispatch' actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:36:in `call' journey (1.0.3) lib/journey/router.rb:68:in `block in call' journey (1.0.3) lib/journey/router.rb:56:in `each' journey (1.0.3) lib/journey/router.rb:56:in `call' actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call' omniauth (1.1.0) lib/omniauth/strategy.rb:392:in `call_app!' omniauth (1.1.0) lib/omniauth/strategy.rb:354:in `callback_phase' omniauth-oauth2 (1.0.2) lib/omniauth/strategies/oauth2.rb:65:in `callback_phase' omniauth (1.1.0) lib/omniauth/strategy.rb:219:in `callback_call' omniauth (1.1.0) lib/omniauth/strategy.rb:175:in `call!' omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call' omniauth (1.1.0) lib/omniauth/strategy.rb:177:in `call!' omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call' omniauth (1.1.0) lib/omniauth/builder.rb:48:in `call' warden (1.1.1) lib/warden/manager.rb:35:in `block in call' warden (1.1.1) lib/warden/manager.rb:34:in `catch' warden (1.1.1) lib/warden/manager.rb:34:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.4.1) lib/rack/etag.rb:23:in `call' rack (1.4.1) lib/rack/conditionalget.rb:25:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/head.rb:14:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/flash.rb:242:in `call' rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/cookies.rb:338:in `call' activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call' activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__2824763875952710462__call__1302249347533121180__callbacks' activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app' railties (3.2.6) lib/rails/rack/logger.rb:16:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call' rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' rack (1.4.1) lib/rack/runtime.rb:17:in `call' activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.4.1) lib/rack/lock.rb:15:in `call' actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call' railties (3.2.6) lib/rails/engine.rb:479:in `call' railties (3.2.6) lib/rails/application.rb:220:in `call' rack (1.4.1) lib/rack/content_length.rb:14:in `call' railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call' rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' /Users/felixthea/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' /Users/felixthea/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' /Users/felixthea/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' 和authentications_controller.rb文件: class AuthenticationsController < ApplicationController def index @authentications = current_user.authentications if current_user end def create omniauth = request.env["omniauth.auth"] authentication = Authentication.find_by_provider_and_uid(omniauth['provider'],omniauth['uid']) if authentication flash[:notice] = "Signed in successfully." sign_in_and_redirect(:user,authentication.user) elsif current_user current_user.authentications.create!(:provider => omniauth['provider'],:uid => omniauth['uid']) flash[:notice] = "Authentication successful." redirect_to authentications_url else user = User.new user.apply_omniauth(omniauth) if user.save flash[:notice] = "Signed in successfully." sign_in_and_redirect(:user,user) else session[:omniauth] = omniauth.except('extra') redirect_to new_user_registration_url end end end def destroy @authentication = current_user.authentications.find(params[:id]) @authentication.destroy redirect_to authentications_url,:notice => "Successfully destroyed authentication." end protected # This is necessary since Rails 3.0.4 # See https://github.com/intridea/omniauth/issues/185 # and http://www.arailsdemo.com/posts/44 def handle_unverified_request true end end 任何帮助,将不胜感激! 谢谢 解决方法
您正在使用旧教程版本.
如果您使用的是2.0或更高版本,则步骤为: config / initializers / devise.rb中的1o require "omniauth-facebook" config.omniauth :facebook,"YOUR_APP_ID","YOUR_APP_SECRET",:strategy_class => OmniAuth::Strategies::Facebook 2o你必须安装这个宝石: gem 'omniauth' gem 'omniauth-facebook' gem 'oauth2' 你在user.rb模型添加模块3o:omniauthable …… class User # Include default devise modules. Others available are: # :token_authenticatable,:encryptable,:confirmable,:lockable,:timeoutable,:omniauthable and :invitable devise :database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable,:omniauthable end 在您的routes.rb中4o devise_for :users,:controllers => { :omniauth_callbacks => "users/omniauth_callbacks",:registrations => "registrations" } do 5o在您的控制器文件夹中创建文件夹用户,并且必须在app / controllers / users / omniauth_callbacks_controller.rb中创建文件之后 在这个omniauth_callbacks_controller.rb中 class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def facebook # with this code you can see the data sent by facebook omniauth = request.env["omniauth.auth"] @user = User.find_for_facebook_oauth(request.env["omniauth.auth"],current_user) if @user.persisted? flash[:notice] = I18n.t "devise.omniauth_callbacks.success",:kind => "Facebook" sign_in_and_redirect @user,:event => :authentication else session["devise.facebook_data"] = request.env["omniauth.auth"] redirect_to new_user_registration_url end end end 6o在你的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.create(name:auth.extra.raw_info.name,provider:auth.provider,uid:auth.uid,email:auth.info.email,password:Devise.friendly_token[0,20] ) end user end def self.new_with_session(params,session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"] user.email = data["email"] end end end 7o最后你必须添加一个链接来连接facebook: 在您的devise / registrations / new.html.erb中,您必须添加: <%= link_to "Sign in with Facebook",user_omniauth_authorize_path(:facebook) %> 就这样.我希望它会有所帮助!问候 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |