简体   繁体   English

current_user未存储在生产服务器上的会话中

[英]current_user not stored in session on production server

I'm having a difference between dev and production (on a shared webserver). 我的开发和生产(在共享的Web服务器上)之间存在差异。 In my application controller I have these 2 lines as part of a method which stores updates: 在我的应用程序控制器中,我将这两行作为存储更新的方法的一部分:

@activity.update_attribute(:user_id, current_user.id)
@activity.update_attribute(:company_id, current_user.company.id)

In dev both the user_id and company_id are known and get saved in the database. 在开发人员中,user_id和company_id都是已知的,并保存在数据库中。 This is because there is a before filter which stores the session info in the current_user. 这是因为有一个before过滤器将会话信息存储在current_user中。

But with the exact same code on the shared production server, it gives an error when it stores the record to the database and when I look in the log these both values are NULL. 但是,如果使用共享生产服务器上的完全相同的代码,则在将记录存储到数据库时以及当我在日志中查看这两个值均为NULL时都会出错。 The error is: 错误是:

"ActiveRecord::StatementInvalid (Mysql2::Error: Column 'company_id' cannot be null: INSERT INTO `activities` (`activity`, `company_id`, `created_at`, `updated_at`, `user_id`) VALUES ('Test', NULL, '2011-12-09 09:01:29', '2011-12-09 09:01:29', NULL)):"

To me it seems on production the session isn't stored properly. 在我看来,会话在生产中存储不正确。 I've searching around and the only think I can find is this article, which says you have to add "allow_http_basic_auth" to the user model. 我到处搜索,唯一能找到的就是这篇文章,它说您必须在用户模型中添加“ allow_http_basic_auth”。 When I do that (I'm not using Authlogic by the way), then it returns this error: 当我这样做时(我不是在使用Authlogic),那么它将返回此错误:

undefined method `allow_http_basic_auth' for #<User:0x007f91990efce0>

Any ideas would be great! 任何想法都很棒!

Edit: in the application controller I have this: 编辑:在应用程序控制器中,我有这个:

  helper_method :current_user

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

    # Save new activity/update 
    def store_update(activity)
     @activity = Activity.new
     @activity.update_attribute(:activity, activity)
     @activity.update_attribute(:user_id, current_user.id)
     @activity.update_attribute(:company_id, current_user.company.id)
     @activity.save
    end

The problem was not is the session, the logger showed correct session values just before saving the record. 问题不是会话,记录器在保存记录之前显示了正确的会话值。 It now works by changing this: 现在可以通过更改以下内容来工作:

@activity = Activity.new
@activity.update_attribute(:activity, activity)
@activity.update_attribute(:user_id, current_user.id)
@activity.update_attribute(:company_id, current_user.company.id)
@activity.save

To this: 对此:

@activity = Activity.new
@activity.update_attributes(:activity => activity, :user_id => current_user.id, :company_id => current_user.company.id)

And I'm not completely sure why... 我不确定为什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM