Ruby-on-rails Rails 应用程序中的 Cookie 溢出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9473808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cookie overflow in rails application?
提问by erogol
ActionDispatch::Cookies::CookieOverflow in UsersController#create
ActionDispatch::Cookies::CookieOverflow 在 UsersController#create
I have this error when I try to open the page. I do not know how to debug this error. Do you have any suggestion for this problem?
当我尝试打开页面时出现此错误。我不知道如何调试这个错误。你对这个问题有什么建议吗?
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
回答by AMIC MING
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.
您可以在 cookie 中存储的内容有 4kb 的限制,当 Rails 将您的对象转换为文本以写入 cookie 时,它可能会大于该限制。
Ruby on Rails ActionDispatch::Cookies::CookieOverflowerror
Ruby on RailsActionDispatch::Cookies::CookieOverflow错误
That way this CookieOverflowError occurs.
这样CookieOverflow就会出现这个错误。
The easiest way to solve this one is, you need change your session_store and don't use the cookie_store. You can use the active_record_storeby example.
解决这个问题的最简单方法是,您需要更改 session_store 并且不要使用cookie_store. 您可以使用active_record_storeby 示例。
Here is the steps
这是步骤
Generate a migration that creates the session table
rake db:sessions:createRun the migration
rake db:migrateModify
config/initializers/session_store.rbfrom(App)::Application.config.session_store :cookie_store, :key => 'xxx'to
(App)::Application.config.session_store :active_record_store
生成创建会话表的迁移
rake db:sessions:create运行迁移
rake db:migrate修改
config/initializers/session_store.rb自(App)::Application.config.session_store :cookie_store, :key => 'xxx'到
(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.
完成这三个步骤后,重新启动应用程序。Rails 现在将使用会话表来存储会话数据,您将没有 4kb 的限制。
回答by Alter Lagos
To make the :active_record_storefunctionality works in Rails 4/5, you must add the activerecord-session_storegem to your Gemfile:
要使该:active_record_store功能在 Rails 4/5 中工作,您必须将activerecord-session_storegem添加到您的Gemfile:
gem 'activerecord-session_store'
then run the migration generator:
然后运行迁移生成器:
rails generate active_record:session_migration
rake db:migrate
And finally set your session store in config/initializers/session_store.rb:
最后将您的会话存储设置为config/initializers/session_store.rb:
Rails.application.config.session_store :active_record_store, :key => '_my_app_session'
UPDATE:
更新:
If anyone is receiving a null value in column "session_id" violates not-null constraintmessage in rails 4, there's a workaround in github(not tested). You must to create an initializer with ActiveRecord::SessionStore::Session.attr_accessible :data, :session_id
如果有人null value in column "session_id" violates not-null constraint在 rails 4 中收到消息,github 中有一个解决方法(未测试)。您必须创建一个初始化程序ActiveRecord::SessionStore::Session.attr_accessible :data, :session_id
回答by David Hempy
If you're seeing this, check that you're not blowing up some session data. In my case, it was thousands of the same message pumped into the flash message. Just saying.
如果您看到这一点,请检查您没有破坏某些会话数据。就我而言,它是将数千条相同的消息注入到闪现消息中。就是说。
I'll add that if you think the solution is to make your cookie store bigger (as most of the other answers address), you're probably better off rethinking what you're actually putting in cookies. If you need more than a couple of auth tokens, session ID's, and maybe a few layout/tracking cookies, you're living in the 90's.
我要补充的是,如果您认为解决方案是让您的 cookie 存储更大(正如大多数其他答案所解决的那样),那么您最好重新考虑一下您实际放入 cookie 的内容。如果您需要多个身份验证令牌、会话 ID 以及一些布局/跟踪 cookie,那么您就生活在 90 年代。
回答by My God
the error message clearly indicates the problem with cookie store size that's overflow.
错误消息清楚地表明溢出的 cookie 存储大小问题。
Your sessions (by default in cookie) needs to be moved to Active record store or memcache store to fix this issue.
您的会话(默认在 cookie 中)需要移动到 Active record store 或 memcache store 以解决此问题。
For Databased sessions:
对于数据库会话:
config.action_controller.session_store = :active_record_store
You need to create the session table as below
您需要创建会话表如下
rake db:sessions:create
rake db:migrate
OR
或者
For Memcache sessions:
对于 Memcache 会话:
config.action_controller.session_store = :mem_cache_store
Also you need to setup a mem cache server and configure it as below:
您还需要设置一个内存缓存服务器并将其配置如下:
config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211',
{:namespace => 'myapp123'}
回答by Zack Xu
It's not a good idea to store a model object in the session.
在会话中存储模型对象不是一个好主意。
Check out this railscast on this topic: http://railscasts.com/episodes/13-dangers-of-model-in-session?autoplay=true
查看有关此主题的 Railscast:http://railscasts.com/episodes/13-dangers-of-model-in-session?autoplay=true
It's a better practice to store the id (user's id in this case) inside the session. Then you won't have this problem.
将 id(在本例中为用户的 id)存储在会话中是一种更好的做法。那你就不会有这个问题了。
(See Frederick Cheung comment above also).
(另请参阅上面的 Frederick Cheung 评论)。
回答by cianmce
That error is because you are trying to serialize the user model When storing an object in a cookie, rails will use Marshal.dumpwhich can produce a large amount of content since it's everything on the user record
该错误是因为您正在尝试序列化用户模型在 cookie 中存储对象时,rails 将使用Marshal.dump,它可以生成大量内容,因为它是用户记录中的所有内容
Instead of storing the actual user record with session[:current_user] = usertry just storing the users' ID then have a method a method to look up the user from that
e.g.
与其存储实际的用户记录,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
回答by Artur79
This error appeared for me when I was running a specs. After updating Capybara from 1.x to 2.x. Just rake tmp:clear solved it.
当我运行规范时,这个错误出现在我身上。将 Capybara 从 1.x 更新到 2.x 后。只是 rake tmp:clear 解决了它。

