New FAMILUG

The PyMiers

Tuesday 28 May 2013

Sửa lỗi ActionDispatch::Cookies::CookieOverflow trong Rails 3.x,4

Nguyên nhân do cookie để lưu session này đã đầy (giới hạn là 4KB thì phải?)
Giải pháp: Lưu chỗ session này vào database, Rails có hỗ trợ dùng Active record.

Có 1 số tut hướng dẫn tạo table trong db bằng chạy:
$ rake db:sessions:create
Thực tế mình chạy bị lỗi (Chả hiểu sao - nên viết lại tut này làm chỗ lưu trữ cho sau này).

rake db:sessions:create
rake aborted!
Don't know how to build task 'db:sessions:create'


Để khắc phục lỗi này:
Đầu tiên bạn thêm
gem 'activerecord-session_store'
vào file Gemfile
Rồi chạy
$ bundle install
$ rails generate active_record:session_migration
$ bundle exec rake db:migrate

Lúc này trong db có thêm bảng sessions để lưu session hiện tại
Trong app_directory/config/initializers/session_store.rb
sửa
# APP_NAME::Application.config.session_store :cookie_store, key: '_app_name_session'
Thành:
# APP_NAME::Application.config.session_store :active_record_store

Bây giờ các session đã được lưu lại trong db.

No comments:

Post a Comment