简体   繁体   English

如何在带有会话的Rails控制台中发送请求?

[英]how to send request in rails console with session?

I know I can use app.get and app.post to test controller in Rails console. 我知道我可以使用app.getapp.post在Rails控制台中测试控制器。

But how to set the session ? 但是如何设置session I found there is app.session , but it seems not to preserve its status. 我发现有app.session ,但似乎没有保留它的状态。

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

If you are using RedisStore for your Rails sessions, you can retrieve a session with the following handy method given a session id 如果将RedisStore用于Rails会话,则可以使用给定会话ID的以下便捷方法来检索会话

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

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). 查看tmp / sessions并找到最新文件(在UNIX上为ls -alrt)。 Let's say the file is called tmp/sessions/ruby_sess.8eb9614a7e4e1e3b Open console and type: 假设该文件名为tmp/sessions/ruby_sess.8eb9614a7e4e1e3b打开控制台,然后键入:

 >> 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 在这种情况下,我尝试访问会话中的购物车对象,该对象通过session[:cart] = Cart.new放置在会话中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM