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

时间:2011-12-09 09:11:00

标签: ruby-on-rails

我在开发和制作之间有区别(在共享的网络服务器上)。在我的应用程序控制器中,我将这两行作为存储更新的方法的一部分:

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

在dev中,user_id和company_id都是已知的,并保存在数据库中。这是因为有一个before过滤器,它将会话信息存储在current_user中。

但是在共享生产服务器上使用完全相同的代码时,它会在将记录存储到数据库时出错,而当我查看日志时这两个值都为NULL。错误是:

"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)):"

对我而言,在生产中似乎没有正确存储会话。我一直在寻找,我唯一能想到的就是这篇文章,它说你必须在用户模型中添加“allow_http_basic_auth”。当我这样做时(顺便说一下我没有使用Authlogic),然后它会返回这个错误:

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

任何想法都会很棒!

编辑:在应用程序控制器中我有:

  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

1 个答案:

答案 0 :(得分:0)

问题不在于会话,记录器在保存记录之前显示了正确的会话值。它现在可以改变这个:

@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

对此:

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

我不确定为什么......