Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails sessions stored in database

I have a page that shows a list of events. When the event is clicked it takes you to a login page and it keeps the session id along with the page. When I go to the next page it always sends the last session variable.

<% @events.each do |e| %>
<%= link_to e.event_name, sessions_new_path %>
<%= e.event_start %> - <%= e.event_stop %>
<% session[:event_id] = e.id %>
<%= session[:event_id] %>
<br>
<% end %>

This shows the session[:event_id] is there and it is storing in the variable but when I click on the link it will send the last session[:event_id] of the loop. Any ideas would be helpful. I am looking at option of either passing the variable to the next page or storing it into a database cell, but not sure on how sessions are stored in databases.

like image 554
Jason Milam Avatar asked Sep 12 '25 02:09

Jason Milam


1 Answers

Assuming you are using a supported database with ActiveRecord, you can configure your application to store sessions in the database by editing your_app/config/initializers/session_store.rb:

YourApp::Application.config.session_store :active_record_store

make sure to comment out or remove the line:

# YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session'

Further, there are auth gems that handle redirecting users to protected pages after signing in. If you haven't already, look at Devise and/or authlogic. Personally, I prefer Devise.

like image 168
pduey Avatar answered Sep 15 '25 05:09

pduey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!