1

从LinkedIn成功验证后,我转发到这个控制器,我得到了

未初始化的常量 UsersController::LinkedIn

这是身份验证成功后控件所在的位置。

class UsersController < ApplicationController
  def index
    # get your api keys at https://www.linkedin.com/secure/developer
    client = LinkedIn::Client.new("ddddd", "eeeee")
    request_token = client.request_token(:oauth_callback =>"http://localhost:3000/users/callback")
    session[:rtoken] = request_token.token
    session[:rsecret] = request_token.secret
    redirect_to client.request_token.authorize_url
  end

  def callback
    client = LinkedIn::Client.new("dddd", "eeeeee")
    if session[:atoken].nil?
      pin = params[:oauth_verifier]
      atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
      session[:atoken] = atoken
      session[:asecret] = asecret
    else
      client.authorize_from_access(session[:atoken], session[:asecret])
    end
    @profile = client.profile
    @connections = client.connections
  end
end

完整跟踪:

pp/controllers/users_controller.rb:4:in `index'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.2.1) lib/active_support/callbacks.rb:414:in `_run__434435962__process_action__199225275__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `send'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `send'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument'
activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal.rb:246:in `action'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.3) lib/journey/router.rb:68: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.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
omniauth (1.0.3) lib/omniauth/strategy.rb:168:in `call!'
omniauth (1.0.3) lib/omniauth/strategy.rb:148:in `call'
omniauth (1.0.3) lib/omniauth/builder.rb:42:in `call'
actionpack (3.2.1) 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.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.1) 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.1) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__397314981__call__4__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `send'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `send'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.1) 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.1) 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.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.2.1) lib/rails/engine.rb:479:in `call'
railties (3.2.1) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.4.1) lib/rack/handler/webrick.rb:13:in `run'
rack (1.4.1) lib/rack/server.rb:265:in `start'
railties (3.2.1) lib/rails/commands/server.rb:70:in `start'
railties (3.2.1) lib/rails/commands.rb:55
railties (3.2.1) lib/rails/commands.rb:50:in `tap'
railties (3.2.1) lib/rails/commands.rb:50
script/rails:6:in `require'
script/rails:6

会话控制器:

    class SessionsController < ApplicationController
      def create
        auth = request.env["omniauth.auth"]
        puts "printing"
        puts  auth
        session[:rtoken] = params[:token]
        session[:rsecret] = params[:secret]
        client = LinkedIn::Client.new("cus7tde3g4pe", "jKf6ZYgf8JhMoIJ4")
        if session[:atoken].nil?
          pin = params[:oauth_verifier]
          atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
          session[:atoken] = atoken
          session[:asecret] = asecret
        else
          client.authorize_from_access(session[:atoken], session[:asecret])
        end
        @profile = client.profile
        @connections = client.connections
      end
      #user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
      session[:user_id] = profile.name
      redirect_to root_url, :notice => "Signed in!"
      #end

      def destroy
        session[:user_id] = nil
        redirect_to root_url, :notice => "Signed out!"
      end
    end

Full Trace : 

C:/linkedin/lovelinkedin/app/controllers/sessions_controller.rb:21: undefined local variable or method `session' for Ses
sionsController:Class (NameError)
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
51:in `require'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
51:in `require'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
36:in `load_dependency'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
51:in `require'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:3
59:in `require_or_load'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:3
13:in `depend_on'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
25:in `require_dependency'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:439:in `eager_load!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:438:in `each'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:438:in `eager_load!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:436:in `each'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:436:in `eager_load!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/application/finisher.rb:53
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `instanc
e_exec'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `run'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:55:in `run_ini
tializers'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `each'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `run_ini
tializers'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/application.rb:136:in `initiali
ze!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:in `
send'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:in `
method_missing'
        from C:/linkedin/lovelinkedin/config/environment.rb:5
        from ./test/test_helper.rb:2:in `require'
        from ./test/test_helper.rb:2
        from C:/linkedin/lovelinkedin/test/unit/helpers/linkedin_helper_test.rb:1:in `require'
        from C:/linkedin/lovelinkedin/test/unit/helpers/linkedin_helper_test.rb:1
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:15:in `require
'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:15
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `select'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4
C:/linkedin/lovelinkedin/app/controllers/sessions_controller.rb:21: undefined local variable or method `session' for Ses
sionsController:Class (NameError)
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
51:in `require'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
51:in `require'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
36:in `load_dependency'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
51:in `require'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:3
59:in `require_or_load'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:3
13:in `depend_on'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:2
25:in `require_dependency'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:439:in `eager_load!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:438:in `each'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:438:in `eager_load!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:436:in `each'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/engine.rb:436:in `eager_load!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/application/finisher.rb:53
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `instanc
e_exec'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `run'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:55:in `run_ini
tializers'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `each'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `run_ini
tializers'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/application.rb:136:in `initiali
ze!'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:in `
send'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:in `
method_missing'
        from C:/linkedin/lovelinkedin/config/environment.rb:5
        from ./test/test_helper.rb:2:in `require'
        from ./test/test_helper.rb:2
        from C:/linkedin/lovelinkedin/test/functional/linkedin_controller_test.rb:1:in `require'
        from C:/linkedin/lovelinkedin/test/functional/linkedin_controller_test.rb:1
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:15:in `require
'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:15
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `select'
        from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4

请帮我解决这个问题,因为我是新手。

4

1 回答 1

2

您的 session_controller 问题的更新答案:

您必须在“操作”中使用“会话”,例如您当前的代码:

class SessionsController < ApplicationController
  def create
    # ... some code for create action
  end 

# NOTICE:  these 4 lines of code should be placed in an "action"
#user = User.find_by_provider_and_uid(auth["pr
session[:user_id] = profile.name
redirect_to root_url, :notice => "Signed in!"
#end

  def destroy
    # ... some code for destroy action
  end
end

您应该将 4 行代码移动到一个动作中,以便 Rails 可以重新识别“会话”:

class SessionsController < ApplicationController
  def create
    # ... some code for create action
    session[:user_id] = profile.name
    redirect_to root_url, :notice => "Signed in!"
  end 

==============

上一个问题的答案:

如果您使用的是 Rails3,则必须将每个 ruby​​gems 添加到 Gemfile,例如:

gem "rake", "0.8.7"
gem 'rails', '3.0.5'
gem 'linkedin'

在这里,通过添加gem 'linkedin',ruby 代码能够包含来自“linkedin”gem 的所有内容。

不推荐使用的 Rails 2.x 方式:如果你不这样做,你必须require在你的控制器中添加语句,例如

# don't do this if you are using Gemfile
require 'linkedin'
class UsersController < ApplicationController
  def index
  # ... 
end 
于 2012-04-10T08:48:11.333 回答