重定向后 Rails 会话不持久 [英] Rails session not persisting after redirects
问题描述
我正在使用会话存储将数据保存到会话中.我的初始化程序/session_store.rb 看起来像:
I am using session store to save data into sessions. My initializers/session_store.rb looks like as:
::Application.config.session_store :active_record_store
向会话代码添加数据:
book = Book.find(1)
session[:abc] = book
我正在另一个页面访问此会话数据.访问会话数据:
I am accessing this session data at a different page. Accessing session data:
book = session[:abc]
问题是我的会话数据在重定向之间没有持久化.可能是什么问题?
Problem is that my session data is not persisted between redirects. What could be the problem?
我可以看到我的会话正在按预期保存数据,但是在重定向发生时它会丢失一些数据,如果再次发生重定向,它会丢失更多数据.令人沮丧的是.
I can see that my session is saving the data as expected but it lost some data from it when redirect happens and lost even more if again redirect happens. Frustrating it is.
推荐答案
你可以试试这个将图书 ID 传递给会话而不是整个图书对象.
You can try this pass book id to session instead of whole book object.
例如
在页面 A
book = Book.find(1)
session[:abc] = book.id
在页面 B
book = Book.find(session[:abc])
这篇关于重定向后 Rails 会话不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!