Rails session variable is not saving
To store a value in a session, you have to set the value in the session itself. Currently, you're setting instance variables, which will get lost if you're reloading the page:
A better way to do it will be to either set the session variables directly, or not reloading the Window on ajax success:
def set_city_session
#binding.pry
if [1, 2, 3].include? params[:city_id].to_i
session[:city_id] = params[:city_id].to_i
if session[:city_id] == 1
session[:city_name] = "Ростов-на-Дону"
end
if session[:city_id] == 2
session[:city_name] = "Ставрополь"
end
if session[:city_id] == 3
session[:city_name] = "Краснодар"
end
end
#binding.pry
end
$("#city-name-link").text("#{escape_javascript(session[:city_name])}");
If you'd like to set a "default" value for the sessions, you'd be best doing something like this:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
before_action :set_session
private
def set_session
session[:city_id] ||= "1"
session[:city_name] ||= "Ростов-на-Дону"
end
end
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 sessions not storing
It appears I was trying to store the session before save.
I switched
def create
@lead = Spree::Lead.new(params[:lead])
session[:newsletter] = "true" if params[:referrer] == "newsletter"
if @lead.save
(rest of code)
to
def create
@lead = Spree::Lead.new(params[:lead])
if @lead.save
session[:newsletter] = "true" if @lead.referrer == "newsletter"
(rest of code)
"Haru, you are such a ninny"
-Chris Farley, Beverly Hills Ninja
Related Topics
Testing HTML5 File Upload with Capybara/Selenium Webdriver - Ruby
How to Build a Docker Image for a Ruby Project Without Build Tools
Ruby: Length of a Line of a File in Bytes
How to Count Duplicates Hash Itens in Ruby 1.8.5 ( Sketchup Ruby API )
Flatten a Ruby Array Without Using Built-In 'Flatten' Method
How to Create an Association Between Two Rails Models
Sublime Text Can't Understand Gets.Chomp
How to Handle Combination []+= for Auto-Vivifying Hash in Ruby
Initialize the Delayed Jobs Gem by Starting the Workers on Application Start
How to Get a List from a Ruby Enumerable
Undefined Method 'Email' for Nil:Nilclass in Exibe the Mail of Table Father