如何使用会话在rails控制台中发送请求?

发布于 2024-12-15 09:54:25 字数 253 浏览 1 评论 0原文

我知道我可以使用 app.getapp.post 在 Rails 控制台中测试控制器。

但是如何设置会话呢?我发现有app.session,但它似乎没有保留其状态。

   $ app.session[:a] = "123"
   $ app.get "/"
   $ app.session
   => {}

I know I can use app.get and app.post to test controller in Rails console.

But how to set the session? I found there is app.session, but it seems not to preserve its status.

   $ app.session[:a] = "123"
   $ app.get "/"
   $ app.session
   => {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一腔孤↑勇 2024-12-22 09:54:25

如果您将 RedisStore 用于 Rails 会话,则可以使用以下方便的方法在给定会话 ID 的情况下检索会话

def get_session(sid)
  store = ActionDispatch::Session::RedisStore.new(
      Rails.application,
      Rails.application.config.session_options)
  store.get_session({}, sid)
end

If you are using RedisStore for your Rails sessions, you can retrieve a session with the following handy method given a session id

def get_session(sid)
  store = ActionDispatch::Session::RedisStore.new(
      Rails.application,
      Rails.application.config.session_options)
  store.get_session({}, sid)
end
記憶穿過時間隧道 2024-12-22 09:54:25

这是一篇博客文章 关于它。从它的乏味程度来看,我猜测人们通常不会这样做。

查看 tmp/sessions 并找到最新的文件(UNIX 上为 ls -alrt)。
假设该文件名为 tmp/sessions/ruby_sess.8eb9614a7e4e1e3b
打开控制台并输入:

<前><代码>>>会话 = Marshal.load(File.open('tmp/sessions/ruby_sess.8eb9614a7e4e1e3b'))
=> {“哈希”=>{:购物车=...
>>>购物车 = 会话["哈希"][:购物车]
....

在本例中,我尝试访问会话中的购物车对象,该对象通过 session[:cart] = 放置在会话中
购物车.new

Here's a blog post about it. Judging by how tedious it is my guess is that people usually don't do this.

Look in tmp/sessions and find the most recent file (ls -alrt on UNIX).
Let's say the file is called tmp/sessions/ruby_sess.8eb9614a7e4e1e3b
Open console and type:

>> session = Marshal.load(File.open('tmp/sessions/ruby_sess.8eb9614a7e4e1e3b'))
=> {"hash"=>{:cart=...
>> cart = session["hash"][:cart]
.... 

In this case I was trying to access a cart object in the session, which was placed in the session with session[:cart] =
Cart.new

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文