I am writing a rails app that involves a cart system stored in session. I did some googling and it turned out I can store the ID of my cart in the session hash like this session[:cart_id]. However, I noticed that if I open a new tab in my rails app, it still uses the same session. This means that multiple tabs of the app share the same cart. I don't want this to happen. Ideally, each browser should get its own cart. Is there any way I can resolve this?
Thanks!
If you're on the same browser, you'll always have the same session.
In Rails, the concept of session is implemented through cookies. And cookies are stored client-side on the browser, specific to each website.
(I could be entirely wrong, but I'd like to think the answer is no.)
Related
I want to store a specific query in session and want to access it when the call made the same method again.
In the beginning of the method I have this:
reports_query = session[:reports_query]
puts reports_query
Just before the end of the method, I have:
session[:reports_query] = query
Nothing gets printed so far. I've also tried cookies instead of session. Doesn't work. I have checked for cookie in browser. I see an auto generated string.
I have made no change in configuration. Everything is generated by rails.
Any help would be appreciative.
Update
I think, I found the reason. Everytime, I refresh the page, I get a new cookie. Is this the reason? If so, how can I have persistent cookie?
This is more of a comment, but I don't have enough rep yet to do that. Anyways, I tested this locally on a Rails 4 app with the default session configuration and I also see the cookie value change on each page reload. I am able to access session data across controllers just fine so I don't think that's your problem.
I have ror app where i can poll. How to remember user without registration? That he could not vote many times for the same answer.
What should I use? I need to create a new model for users? Maybe use some gems?
Every visitor to your Rails website will be given a session, you can just store some state in there like: session[:seen] = true and in subsequent requests you can check for the existence of that session variable.
See here for more and especially note that a user can always thwart this effort by clearing their cookies (which is why it would be better to use registration).
I've got an application where a customer can only have one session at any one time. I've got it sorted so this is the case except in one scenario where when a tab or a browser is closed the session cookie is not destroyed. This then lets them log in elsewhere, and then reopen the browser and lets them have multiple sessions running.
If I properly close the browser down then the cookie is removed, but is is possible to remove them without a full closedown of the browser?
The application is already live on a customers site, so I'm hopefully looking for a way to just destroy the cookie rather than re-writing how the whole session storage currently works, though any suggestion is welcome.
I know this post is really old, but someone could find this useful, here there is a gem that lets manage a new level of sessions per tab -> https://github.com/santiagodoldan/micro_sessions, I forked from another repo and added support for rails 4.x
We have some sharing elements of our application where we embed logging IDs into the URL's that we share out. When a user clicks that URL, we add a record to our database so we can hopefully follow them throughout the registration process. We've found that if you reset your browser and go to the link the first time, there is no session info from the controller. However, all subsequent requests then have the session. It's almost like it's getting created after the first request.
We attempted to log it via ajax on the view, but this is cumbersome in all the places we want.
Anyone know what sessions wouldn't be available in the controller on first access for a new uesr?
I think you might have code in the wrong order. You must have the session creation before any logic can my applied to it.
Hope this helps.
Warning: some of this may be very wrong-headed, so please let me know if my assumptions are incorrect.
Here's what I'm trying to accomplish:
I'm using restful-authentication for login. However, as I am using flex/ruby_amf for my UI, I have to separately authenticate each connection from flex.
The way I decided to do that was by having the log-in screen redirect to the embedded flash page, inserting the session-id as a flashvar. The flash app sends the session-id with every request, and a before filter on all of the relevant controllers checks to see if the user associated with the session identified by the session-id is logged on.
The way I associate a user with session is by adding a 'user_id' column to the sessions table, and doing an sql "update sessions set user_id...'" type query called from the login function.
However, the user_id only gets updated the 2nd time the user logs in. A little investigating showed that the record in the sessions table does not yet exist during execution of the login function.
So, if everything up to this point makes sense, and conforms to best-practices, etc., then my question is:
At what point in time is the record in the sessions table created? Is there a way to update the session object in the login function and have rails write the user_id to the database for me?
The behavior of sessions in rails is a real mystery to me. I'd appreciate any help.
Thank you.
In Rails 2.3, the session is saved after the Rack application has finished its processing. In traditional Rails applications, this will be after the request is fully processed: before filters, controller action, view rendering, and after filters. Look in actionpack/lib/action_dispatch/vendor/rack-1.1.pre/rack/session/abstract/id.rb.
If you think about it, this makes perfect sense. Writing the session to its store every time you place something in the session would incur a lot of extra overhead.
It's Rails, so if you want to mess with it enough, sure, you can monkeypatch yourself a way to write the session to store anytime you wish. I don't recommend it. You'll end up having to rework the code constantly as Rails evolves.
You are right that for ActiveRecord::SessionStore, one row does map to one session. The data column is an encoded form of every object you put in the session. Each time a request comes in, Rails has to reconstitute the session as it existed by creating new instances of all the objects you previously stored in it.