Ruby on rails 无法跨控制器访问会话变量

Ruby on rails 无法跨控制器访问会话变量,ruby-on-rails,session,Ruby On Rails,Session,我是ROR初学者,我正在尝试在我的ROR应用程序中使用omniauth openid身份验证来实现一个基本的Google openid。以下是一些代码片段: 会话\u controller.rb: 应用程序控制器: 模板: <div id="user-widget"> <%= current_user %> </div> 下面是一个人在网站上签名时日志的样子: Started GET "/auth/google" for 127.0.0.1 at 201

我是ROR初学者,我正在尝试在我的ROR应用程序中使用omniauth openid身份验证来实现一个基本的Google openid。以下是一些代码片段:

会话\u controller.rb:

应用程序控制器:

模板:

<div id="user-widget">
  <%= current_user %>
</div>
下面是一个人在网站上签名时日志的样子:

Started GET "/auth/google" for 127.0.0.1 at 2014-04-07 00:44:25 +0530
(google) Request phase initiated.
I, [2014-04-07T00:44:25.150729 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/id without verifying server certificate; no CA path was specified.
I, [2014-04-07T00:44:25.692758 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/ud without verifying server certificate; no CA path was specified.
I, [2014-04-07T00:44:26.192769 #2047]  INFO -- OpenID: Generated checkid_setup request to https://www.google.com/accounts/o8/ud using stateless mode.


Started GET "/auth/google/callback?_method=post&<some_secrets>" for 127.0.0.1 at 2014-04-07 00:44:30 +0530
(google) Callback phase initiated.
I, [2014-04-07T00:44:30.588217 #2047]  INFO -- OpenID: Error attempting to use stored discovery information: OpenID::TypeURIMismatch
I, [2014-04-07T00:44:30.588282 #2047]  INFO -- OpenID: Attempting discovery to verify endpoint
I, [2014-04-07T00:44:30.588308 #2047]  INFO -- OpenID: Performing discovery on https://www.google.com/accounts/o8/id?id=AItOawkBno-m7mK0IH6jWkV1hl2xQtNLce1VdXI
I, [2014-04-07T00:44:30.588624 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/id?id=AItOawkBno-m7mK0IH6jWkV1hl2xQtNLce1VdXI without verifying server certificate; no CA path was specified.
I, [2014-04-07T00:44:31.080153 #2047]  INFO -- OpenID: Using 'check_authentication' with https://www.google.com/accounts/o8/ud
I, [2014-04-07T00:44:31.081077 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/ud without verifying server certificate; no CA path was specified.
Processing by SessionsController#create as HTML
  Parameters: {"provider"=>"google"}
Can't verify CSRF token authenticity
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."provider" = 'google' AND "users"."uid" = 'https://www.google.com/accounts/o8/id?id=AItOawkBno-m7mK0IH6jWkV1hl2xQtNLce1VdXI' ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Session[:user_id] =
1
Session[:my] =
some
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 77ms (ActiveRecord: 1.0ms)


Started GET "/" for 127.0.0.1 at 2014-04-07 00:44:31 +0530
Processing by WelcomeController#index as HTML
  Rendered welcome/index.html.erb within layouts/application (0.1ms)
Completed 500 Internal Server Error in 5ms

ActiveRecord::RecordNotFound (Couldn't find User without an ID):
  app/controllers/application_controller.rb:8:in `current_user'
  app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__87755315857576517_2161879520'


  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.0ms)

因此,在会话控制器中填充的会话变量在应用程序控制器中基本上是不可访问的。

哪一个是您的会话存储?ActiveRecord::RecordNotFound找不到没有ID的用户非常清楚,在这段时间内,您似乎没有将用户存储在数据库中,但您对其进行了查找。您使用的是同一个域吗?如果将localhost用于回调,将127.0.0.1用于root,则会话将使用不同的会话\u id并具有不同的内容。@tirdadc:user.id正在被存储。日志没有显示这一点,因为我在登录时第二次使用了同一个用户。它第一次存储用户并继续加载。User.Find1为我提供了一个有效的用户对象。另外,session[:user _id]变量始终为空。我每次都得到一个空字符串@斯塔夫罗斯:我还没有在初始化文件中定义任何会话存储。我猜它默认与cookie存储一起使用。如果这可能是一个问题,我可以阅读更多关于这方面的内容。谢谢
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery

  private
  def current_user
    @current_user = User.find(session[:user_id]) 
  end

  helper_method :current_user
end
<div id="user-widget">
  <%= current_user %>
</div>
Started GET "/auth/google" for 127.0.0.1 at 2014-04-07 00:44:25 +0530
(google) Request phase initiated.
I, [2014-04-07T00:44:25.150729 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/id without verifying server certificate; no CA path was specified.
I, [2014-04-07T00:44:25.692758 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/ud without verifying server certificate; no CA path was specified.
I, [2014-04-07T00:44:26.192769 #2047]  INFO -- OpenID: Generated checkid_setup request to https://www.google.com/accounts/o8/ud using stateless mode.


Started GET "/auth/google/callback?_method=post&<some_secrets>" for 127.0.0.1 at 2014-04-07 00:44:30 +0530
(google) Callback phase initiated.
I, [2014-04-07T00:44:30.588217 #2047]  INFO -- OpenID: Error attempting to use stored discovery information: OpenID::TypeURIMismatch
I, [2014-04-07T00:44:30.588282 #2047]  INFO -- OpenID: Attempting discovery to verify endpoint
I, [2014-04-07T00:44:30.588308 #2047]  INFO -- OpenID: Performing discovery on https://www.google.com/accounts/o8/id?id=AItOawkBno-m7mK0IH6jWkV1hl2xQtNLce1VdXI
I, [2014-04-07T00:44:30.588624 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/id?id=AItOawkBno-m7mK0IH6jWkV1hl2xQtNLce1VdXI without verifying server certificate; no CA path was specified.
I, [2014-04-07T00:44:31.080153 #2047]  INFO -- OpenID: Using 'check_authentication' with https://www.google.com/accounts/o8/ud
I, [2014-04-07T00:44:31.081077 #2047]  INFO -- OpenID: WARNING: making https request to https://www.google.com/accounts/o8/ud without verifying server certificate; no CA path was specified.
Processing by SessionsController#create as HTML
  Parameters: {"provider"=>"google"}
Can't verify CSRF token authenticity
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."provider" = 'google' AND "users"."uid" = 'https://www.google.com/accounts/o8/id?id=AItOawkBno-m7mK0IH6jWkV1hl2xQtNLce1VdXI' ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Session[:user_id] =
1
Session[:my] =
some
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 77ms (ActiveRecord: 1.0ms)


Started GET "/" for 127.0.0.1 at 2014-04-07 00:44:31 +0530
Processing by WelcomeController#index as HTML
  Rendered welcome/index.html.erb within layouts/application (0.1ms)
Completed 500 Internal Server Error in 5ms

ActiveRecord::RecordNotFound (Couldn't find User without an ID):
  app/controllers/application_controller.rb:8:in `current_user'
  app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__87755315857576517_2161879520'


  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
  Rendered /Users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.0ms)