Rails session does not persist?

Zaharia Anton
1 min readNov 29, 2020

Off course not if your project structure it’s the same as my one. If rails new it’s used with --api flag then it won’t store the session hash after a refresh. It gave me a hard time because I was checking with byebug in my create session method:

def create
@user = User.find_by(email: params[:email])
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
render json: {
status: :created,
logged_in: true,
user: @user
}
else
render json: {status: 401}
end
byebug
end

In the byebug console my session[:user_id] had the right value, but, it was not persisted. After hours spent on google and stackoverflow I found the solution. In my config/application.rb file, inside the Application class I added the following:

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, key: '_backend_chat_app'

That sorted the problem, and after a refresh, my session hash is still there.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (1)

Write a response

GOD BLESS YOU

--