Rails 3:在 Active Record(不是 cookie)中存储会话 [英] Rails 3: Storing Session in Active Record (not cookie)

查看:40
本文介绍了Rails 3:在 Active Record(不是 cookie)中存储会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌大学搜索了大约一个小时左右,但没有关于这个主题的单一好的文档:-(希望有人能提供帮助.我愿意买一本书,只要有人能告诉我哪个.

我正在使用以下版本:

  • 导轨 3.2.6,
  • ruby 1.9.3p194(2012-04-20 修订版 35410)[x86_64-darwin11.4.0]

要开始在 Active Record 中使用会话存储而不是 cookie,我执行了以下操作:

To start using session storage in Active Record instead of cookie, I did following:

  1. 更新config/initializers/session_store.rb.注释第一行并取消注释最后一行,所以我有:

# Be sure to restart your server when you modify this file.

# Myapp::Application.config.session_store :cookie_store, key: '_elegato_session'

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
Myapp::Application.config.session_store :active_record_store

  • rake db:sessions:create

    invoke  active_record
      create    db/migrate/20120729025112_add_sessions_table.rb
    

  • rake db:migrate

    ==  CreateLinkedinUsers: migrating ============================================
    -- create_table(:linkedin_users)
      -> 0.0236s
    ==  CreateLinkedinUsers: migrated (0.0237s) ===================================
    
    ==  AddSessionsTable: migrating ===============================================
    -- create_table(:sessions)
      -> 0.0012s
    -- add_index(:sessions, :session_id)
      -> 0.0006s
    -- add_index(:sessions, :updated_at)
      -> 0.0006s
    ==  AddSessionsTable: migrated (0.0026s) ======================================
    

  • 为了找出实际创建的表,我打开sqlite3文件

  • To find out what table is actually created, I open the sqlite3 file

    sqlite> .schema sessions
    CREATE TABLE "sessions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "session_id"   varchar(255) NOT NULL, "data" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
    CREATE INDEX "index_sessions_on_session_id" ON "sessions" ("session_id");
    CREATE INDEX "index_sessions_on_updated_at" ON "sessions" ("updated_at");
    

  • 因此,会话表中有一个数据"字段,用于存储与会话相关的所有内容.

    So, there is a "data" field in sessions table that will store everything related to the session.

    我需要在会话中存储 2 个变量的值:access_token 和 request_token.我可以使用以下吗?(我以前为 cookie 存储会话值的方式)

    I need to store values for 2 variables in session: access_token and request_token. Can I use following? (the way I used to store session values for a cookie)

    session[:access_token] = <blah>
    session[:request_token] = <some other blah?
    

    如果确实有效,ruby 是否会将两个变量作为数组存储在表的数据"字段中.

    And if it does work, does ruby store both variables in the "data" field of the table as an array.

    非常感谢您的帮助!

    推荐答案

    您的所有会话变量都在会话表的数据列中使用 Base64 进行序列化.如果您使用 cookie 会话存储分配和检索值,您可以像使用它一样使用它.在数据库中存储会话变量有一些很好的好处,比如只在每个请求期间传输 session_id,而不是来自 cookie 的整个会话,通过检查它们的年龄(created_at/updated_at)来管理陈旧或旧会话的能力,以及能够存储比 cookie 中更多的信息.

    All of your session variables are serialized using Base64 in the data column of the sessions table. You can use it the same way as you would use it if you were assigning and retrieving values using the cookie session store. There are some nice benefits to storing session variables in the db, like only transmitting the session_id during each request, rather than the entire session from the cookie, the ability to manage stale or old sessions by checking their age (created_at/updated_at), and the ability to store more info than you can in a cookie.

    您也可以提供自己的 Session 类,请查看 SessionStore 类中的文档:https://github.com/rails/rails/blob/5edfc463484827df364a1e589677d5c84dfac282/activerecord/lib/active_record/session_store.rb

    You can provide your own Session class also, check the documentation in the SessionStore class: https://github.com/rails/rails/blob/5edfc463484827df364a1e589677d5c84dfac282/activerecord/lib/active_record/session_store.rb

    这篇关于Rails 3:在 Active Record(不是 cookie)中存储会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆