use*_*036 13 session ruby-on-rails
我拖了谷歌大学大约一个小时左右,但没有一个关于这个主题的好文档:-(希望有人可以提供帮助.我愿意买一本书,只要有人能告诉我哪一本.
我使用以下版本:
要开始在Active Record中使用会话存储而不是cookie,我做了以下操作:
更新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
Run Code Online (Sandbox Code Playgroud)rake db:sessions:create
invoke active_record
create db/migrate/20120729025112_add_sessions_table.rb
Run Code Online (Sandbox Code Playgroud)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) ======================================
Run Code Online (Sandbox Code Playgroud)要找出实际创建的表,我打开sqlite3文件
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");
Run Code Online (Sandbox Code Playgroud)因此,会话表中有一个"数据"字段,用于存储与会话相关的所有内容.
我需要在session中存储2个变量的值:access_token和request_token.我可以使用以下?(我用来存储cookie的会话值的方式)
session[:access_token] = <blah>
session[:request_token] = <some other blah?
Run Code Online (Sandbox Code Playgroud)
如果确实有效,ruby会将表中"data"字段中的两个变量存储为数组.
非常感谢您的帮助!
您通常不必太关心 Rails 如何存储会话,它是透明的。如果您使用 cookie 存储或 ActiveRecord 存储,您的代码将不会更改。
所以是的,你可以做你之前做的事情。从技术上讲,包含会话的序列化字符串存储在data字段中,但这并不重要:)
不过,我建议使用 cookie 存储,除非您有充分的理由使用 ActiveRecord(会话中的数据超过 4K?)
| 归档时间: |
|
| 查看次数: |
11707 次 |
| 最近记录: |