Rails 6 session variables not persisting
Ok, found the problem. Turns out that I had copied the setting config.session_store :cache_store
in development.rb
from a different project I had been working on. However, this setting was added as part of the StimulusReflex setup for that other project.
From the StimulusReflex docs:
Cookie-based session storage is not currently supported by StimulusReflex.
Instead, we enable caching in the development environment so that we can assign our user session data to be managed by the cache store.
The default setting for this option is cookie_store
. By changing it to :cache_store
without specifying a cache repo, it implements ActionDispatch::Session::CacheStore and defaults to storing it in Rails.cache
, which uses the :file_store
option, which dumps it in tmp/cache
.
However, further down in development.rb
, there is some conditional logic that assigns config.cache_store
to :null_store
if there is no caching-dev.txt
file. This implements ActiveSupport::Cache::NullStore, which is "a cache store implementation which doesn't actually store anything."
So because I had not enabled caching with rails dev:cache
for this project, the session cache was getting toasted with every request.
LESSON LEARNED: Be very careful when copying config settings from an old project to a new one!
Rails session not persisting after redirects
You can try this
pass book id to session instead of whole book object.
e.g.
in page A
book = Book.find(1)
session[:abc] = book.id
in page B
book = Book.find(session[:abc])
Rails 5 sessions not persisting
I found out what my problem was. In my application.html.erb, my logout link was incorrect and was forcing my user to logout immediately after logging them in.
I had <%= link_to "LOGOUT", logout %>
when it should've been <%= link_to "LOGOUT", logout_path, method: :delete %>
.
Rails - Session Variable Not Persisting Through Apartment Switch
Found the answer here: Share session (cookies) between subdomains in Rails?
The problem was in our session_store.rb. We weren't taking into account that by setting domain: :all
, we were disregarding the default TLD length of 1. We were using lvh.me instead of localhost.
Hope this helps others!
Related Topics
Ruby: How to Process a CSV File with "Bad Commas"
Differencebetween Def Func(Var) and Def Func=(Var)
Can Ruby Tell If It Is Called from an Interactive Shell or Cron
Sum of All Amount If Charge Dates Are the Same in Stripe
Make a Ruby Script with Text Io Double Clickable Executable File
Ruby Webrick Http Authentication
How to Sign Out in a Rails App, Using Devise Gem, No Route Matches /Users/Sign_Out
How to Extract Specific Elements from an Array
Ruby on Rails - Helper Method - Undefined Method 'Log_In' in Ruby on Rails
Heroku Deplyoment Asset Precompiling Failed on Rails 6
Importing CSV as Test Data in Cucumber
Ruby on Rails - Error Making New App (Rb:55:In 'Require')
Ruby Implementation Win32API Get Mouse Scroll/Wheel Input
Count Overlapping Regex Matches in Perl or Ruby
Find Values in Common Between Two Arrays
How to Keep Sending Emails to Users Every Week Depending on User Date Input in Rails