Sharing session variables across Controllers?

527 views
Skip to first unread message

Myth17

unread,
Mar 8, 2012, 9:38:02 AM3/8/12
to rubyonra...@googlegroups.com
I have a couple of session parameters set in a Controller which I want to use in a different Controller. Which is the cleanest way to do this?

I did quite a bit of Googling but could not come up with anything substancial.

Tim Shaffer

unread,
Mar 8, 2012, 9:58:47 AM3/8/12
to rubyonra...@googlegroups.com
Session variables should be shared, unless I'm not understanding the question.

You can set a session variable in one controller like so:

session[:my_key] = 'my value'

Then access it in another controller by simply calling session[:my_key]

Myth17

unread,
Mar 8, 2012, 10:06:52 AM3/8/12
to rubyonra...@googlegroups.com
In my application once I call an action on a different controller all my session variables seem to disappear also the login session seems to expire. Even if   I start a new request from browser I need to authenticate again.

Colin Law

unread,
Mar 8, 2012, 10:29:01 AM3/8/12
to rubyonra...@googlegroups.com
On 8 March 2012 15:06, Myth17 <nitish...@gmail.com> wrote:
> In my application once I call an action on a different controller all my
> session variables seem to disappear also the login session seems to expire.
> Even if   I start a new request from browser I need to authenticate again.

Something is clearing the session then. Do a global search in the
code for session and see if you clear it anywhere. If you find it
cleared anywhere, even if you think that code should not be called,
then put some debug in to check (or just comment out the clear for
temporarily).

Colin

>
>
> On Thursday, March 8, 2012 8:28:47 PM UTC+5:30, Tim Shaffer wrote:
>>
>> Session variables should be shared, unless I'm not understanding the
>> question.
>>
>> You can set a session variable in one controller like so:
>>
>> session[:my_key] = 'my value'
>>
>> Then access it in another controller by simply calling session[:my_key]
>

> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/G91C6M2c0XAJ.
>
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.

--
gplus.to/clanlaw

Myth17

unread,
Mar 8, 2012, 10:47:08 AM3/8/12
to rubyonra...@googlegroups.com
Its actually weird then, I did a search to double check but I am not clearing the session anywhere. :|


On Thursday, March 8, 2012 8:59:01 PM UTC+5:30, Colin Law wrote:
On 8 March 2012 15:06, Myth17 <nitish...@gmail.com> wrote:
> In my application once I call an action on a different controller all my
> session variables seem to disappear also the login session seems to expire.
> Even if   I start a new request from browser I need to authenticate again.

Something is clearing the session then.  Do a global search in the
code for session and see if you clear it anywhere.  If you find it
cleared anywhere, even if you think that code should not be called,
then put some debug in to check (or just comment out the clear for
temporarily).

Colin

>
>
> On Thursday, March 8, 2012 8:28:47 PM UTC+5:30, Tim Shaffer wrote:
>>
>> Session variables should be shared, unless I'm not understanding the
>> question.
>>
>> You can set a session variable in one controller like so:
>>
>> session[:my_key] = 'my value'
>>
>> Then access it in another controller by simply calling session[:my_key]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/G91C6M2c0XAJ.
>

> To post to this group, send email to rubyonrails-talk@googlegroups.com.


> To unsubscribe from this group, send email to

> rubyonrails-talk+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.

--
gplus.to/clanlaw

Myth17

unread,
Mar 8, 2012, 10:53:14 AM3/8/12
to rubyonra...@googlegroups.com
I can see message "WARNING: Can't verify CSRF token authenticity" on the rails console. Could this be the reason?

Colin Law

unread,
Mar 8, 2012, 11:04:13 AM3/8/12
to rubyonra...@googlegroups.com
On 8 March 2012 15:53, Myth17 <nitish...@gmail.com> wrote:
> I can see message "WARNING: Can't verify CSRF token authenticity" on the
> rails console. Could this be the reason?

Yes, probably. Rails is clearing the session because it detects the
problem. Google for the message and you will find lots of hits.

Colin

>>> > To post to this group, send email to rubyonra...@googlegroups.com.


>>> > To unsubscribe from this group, send email to

>>> > rubyonrails-ta...@googlegroups.com.


>>> > For more options, visit this group at
>>> > http://groups.google.com/group/rubyonrails-talk?hl=en.
>>>
>>> --
>>> gplus.to/clanlaw
>

> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/rubyonrails-talk/-/c9d1Cdc7BgYJ.
>
> To post to this group, send email to rubyonra...@googlegroups.com.


> To unsubscribe from this group, send email to

> rubyonrails-ta...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages