Not able to access rails session - ruby-on-rails

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.

Related

Issue with session in Rails

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.)

Store Temporary Sensitive Data In Rails 4

I'm working on a Rails app but i'm having trouble implementing a solution for storing temporary data and automatically clearing it when the user sees it. I thought about using flash but that only works when the user visits via the next request. Im looking for something that will let the user browse the site and view other things but once they get to the page such as /example/, they see the temporary sensitive data, then next time they reload that page or go somewhere else and come back its permanently gone. Any suggestions?
Redis will be fastest and simplest solution (but not such simple as flash notices). Redis have built in lpop method to get something from list and remove it after that. I think you will not find good solution which will not require some setup (e.g. Redis installation)

Ruby on Rails Sessions for new users

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.

To understand a line about a login -variable in a session

What does the following line mean?
Put the boolean variable isLogin to your session such that you check the session each time your user goes to the secured site.
I would like to know how you can put a variable to a session. I know at the abstract level that
session is a semi-permanent
interactive information interchange,
also known as a dialogue, a
conversation or a meeting, between two
or more communicating devices, or
between a computer and user
I know that you can store data in a URL by separating variables by the character &.
I know at the abstract level that you need to use post orget and some read -function to check the data in the URL.
I know that cookies are files where you store data, but I have never stored data to them.
Does he mean that I should put the login -variable to the URL or to cookies?
Taking out all the context doesn't make it any easier to answer your question - actually I have to guess that you are talking about php, because it looks like you might be.
Sessions.
Sessions are a way of 'remembering' users for a limited time. Say I visit page A.php on your website first. Now, that website might define an isLoggedIn session variable for me. If a bit later I go to page B.php on your site, that site 'remembers' that variable and can tell what it' s value was.
Sessions and Cookies do have a relation, but that only matters when you want to know how sessions work. This will be important later on as you will need to know the weaknesses of sessions, but first it is important you get to know how to use them.
Before you can use session variables, you must call session_start(), to start a session - this must be called on each page that uses the session variables. Once we have we can simply access the array $_SESSION and all that's in there will be remembered with the session.
Take a look over here to get a more complete explanation and a number of examples.

Rails w/ ActiveRecord Sessions and Restful-authentication: when does the sessions table get written to?

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.

Resources