Session key value not available in the view

15 views
Skip to first unread message

Sebastjan Hribar

unread,
Oct 14, 2024, 2:53:23 AM10/14/24
to Roda
Hi,

I've encountered another problem, which I can't debug on my own. As the title says, I set a session value in a route, but that is not available in the view. For example, in the login route I have:

      r.post do
        @user = User.first(username: r.params['username'])

        if @user && @user.authenticate(r.params['password'])
          session[:user_id] = @user.id
          flash[:notice] = 'Login successful!'
          r.redirect '/users'
        else
          flash.now[:error] = 'Invalid username or password.'
          view('login')
        end
      end

In my layout.erb the session[:user_id] comes in empty.

I'm using the roda sequel stack and I haven't modified anything about sessions and I don't get any other errors. Am I missing some additional configuration?


Jeremy Evans

unread,
Oct 15, 2024, 1:57:28 AM10/15/24
to ruby...@googlegroups.com
On Sun, Oct 13, 2024 at 11:53 PM Sebastjan Hribar <sebastja...@gmail.com> wrote:
Hi,

I've encountered another problem, which I can't debug on my own. As the title says, I set a session value in a route, but that is not available in the view. For example, in the login route I have:

      r.post do
        @user = User.first(username: r.params['username'])

        if @user && @user.authenticate(r.params['password'])
          session[:user_id] = @user.id
          flash[:notice] = 'Login successful!'
          r.redirect '/users'
        else
          flash.now[:error] = 'Invalid username or password.'
          view('login')
        end
      end

In my layout.erb the session[:user_id] comes in empty.

rosa-sequel-stack defaults to the Roda session plugin, which uses JSON for session storage, and doesn't support symbol keys.  Can you change it to use session['user_id'] and see if that fixes the issue?

Thanks,
Jeremy

Sebastjan Hribar

unread,
Oct 15, 2024, 9:59:42 AM10/15/24
to Roda
Oh my god! It was staring me in the face! I went back and checked Mastering Roda and sure enough it's r.session["name"] and not a symbol.
I apologize...

torek, 15. oktober 2024 ob 07:57:28 UTC+2 je oseba jeremy...@gmail.com napisala:
Reply all
Reply to author
Forward
0 new messages