在轨道应用程序中 Cookie 溢出?

ActionDispatch::Cookies::CookieOverflow in UsersController#create

当我试图打开页面时,出现了这个错误。我不知道如何调试这个错误。你对这个问题有什么建议吗?

def create
@user = User.new(params[:user])
sign_in @user


if @user.save
@user.folders.create(:folder_name=>"Default Folder", :user_id=>@user.id)
flash[:success] = "Welcome to Bunch<it>! "
redirect_to @user
else
@title = "Sign up"
render 'new'
end
end




def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
session[:current_user] = user
current_user = user
end
78906 次浏览

You've got a 4kb limit on what you can store in a cookie, and when Rails converts your object into text for writing to the cookie its probably bigger than that limit.

Ruby on Rails ActionDispatch::Cookies::CookieOverflow错误

这样就会发生 CookieOverflow错误。

解决这个问题的最简单的方法是,您需要更改 session _ store 并且不使用 cookie_store。您可以通过示例使用 active_record_store

这是步骤

  1. 生成创建会话表的迁移

    rake db:sessions:create
    
  2. Run the migration

    rake db:migrate
    
  3. Modify config/initializers/session_store.rb from

    (App)::Application.config.session_store :cookie_store, :key => 'xxx'
    

    to

    (App)::Application.config.session_store :active_record_store
    

Once you’ve done the three steps, restart your application. Rails will now use the sessions table to store session data, and you won’t have the 4kb limit.

错误消息清楚地表明 Cookie 存储大小存在溢出问题。

您的会话(默认情况下在 cookie 中)需要移动到活动记录 存储或 memcache 存储来修复此问题。

数据库会话:

config.action_controller.session_store = :active_record_store

您需要按如下方式创建会话表

rake db:sessions:create
rake db:migrate

或者

对于 Memcache 会话:

config.action_controller.session_store = :mem_cache_store

此外,您还需要设置一个 mem 缓存服务器,并配置如下:

config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211',
{:namespace => 'myapp123'}

在会话中存储模型对象不是一个好主意。

Check out this railscast on this topic: Http://railscasts.com/episodes/13-dangers-of-model-in-session?autoplay=true

更好的做法是将 id (本例中是用户的 id)存储在会话中。 那你就不会有这个问题了。

(见上文张震远评论)。

这个错误出现在我运行一个 spec 时,在将 Capybara 从1.x 更新到2.x 之后。 只是耙 tmp: 明确解决了它。

为了使 :active_record_store的功能在 Rails 4/5中工作,您必须将 Activerrecord-session _ store gem 添加到您的 Gemfile:

gem 'activerecord-session_store'

然后运行迁移生成器:

rails generate active_record:session_migration
rake db:migrate

最后,将会话存储设置为 config/initializers/session_store.rb:

Rails.application.config.session_store :active_record_store, :key => '_my_app_session'

更新:

如果有人在 Rails 4中接收到 null value in column "session_id" violates not-null constraint消息,那么就有一个 在 Github 的变通方法(未经测试)。必须使用 ActiveRecord::SessionStore::Session.attr_accessible :data, :session_id创建初始值设定项

如果您看到这个,请检查您是否正在放大某些会话数据。对我来说,是成千上万条相同的信息被注入到闪存信息中。说说而已。

我要补充的是,如果你认为解决方案是让你的 cookie 存储更大(因为大多数其他的回答地址) ,你可能最好重新考虑你实际上放在 cookie。如果您需要多于两个认证令牌、会话 ID,也许还需要一些布局/跟踪 cookie,那么您生活在90年代。

该错误是因为您试图序列化用户模型 当在 cookie 中存储对象时,Rails 将使用 法警,扔掉,它可以生成大量内容,因为它是用户记录中的所有内容

与使用 session[:current_user] = user存储实际用户记录不同,尝试只存储用户 ID,然后使用一个方法一个方法从中查找用户 例如:。

def sign_in(user)
...
session[:current_user_id] = user.id
end


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

我的问题是因为代码

rescue StandardError => e
flash[:error] = "Error was #{error.message}"
end

error.message太大了