# Persistent Sessions

**URL:** <https://discuss.rubyonrails.org/t/persistent-sessions/49755>\
**Category:** rubyonrails-talk\
**Created:** [February 28, 2010, 3:16am UTC](https://discuss.rubyonrails.org/t/persistent-sessions/49755 "2010-02-28T03:16:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 28, 2010, 3:16am UTC](https://discuss.rubyonrails.org/t/persistent-sessions/49755/1 "2010-02-28T03:16:23Z")

</div>

Hi there,

I am trying to figure out how to have persistent sessions (with like a month until expiration) for my users, sort of like a "remember me" functionality but that is handled automatically on login. I am hoping I can get some help from someone who is more experienced with Rails.

I am currently using a controller called "Sessions" to handle my session properties.

This is what my controller looks like.

def create &nbsp;&nbsp;&nbsp;&nbsp;@current\_user = User.find\_by\_login\_and\_password( &nbsp;&nbsp;&nbsp;&nbsp;params[:login], params[:password])

&nbsp;&nbsp;&nbsp;&nbsp;if @current\_user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:user\_id] = @current\_user.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if session[:return\_to] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to session[:return\_to] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:return\_to] = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to links\_path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> 'new' &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def destroy &nbsp;&nbsp;&nbsp;&nbsp;session[:user\_id] = @current\_user = nil &nbsp;&nbsp;end

I have surfed around the internet looking for solutions but their is no real consistent method out there. Everyone seems to have a different opinion on how to manage sessions/cookies. I am hoping to not have to change my controller structure (by say installing a plug-in) and sticking with Sessions creation.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [February 28, 2010, 3:50am UTC](https://discuss.rubyonrails.org/t/persistent-sessions/49755/2 "2010-02-28T03:50:47Z")

</div>

> Hi there,
> 
> I am trying to figure out how to have persistent sessions (with like a month until expiration) for my users, sort of like a "remember me" functionality but that is handled automatically on login. I am hoping I can get some help from someone who is more experienced with Rails.
> 
> I am currently using a controller called "Sessions" to handle my session properties.
> 
> This is what my controller looks like.
> 
> def create &nbsp;&nbsp;&nbsp;@current\_user = User.find\_by\_login\_and\_password( &nbsp;&nbsp;&nbsp;params[:login], params[:password])
> 
> &nbsp;&nbsp;&nbsp;if @current\_user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:user\_id] = @current\_user.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if session[:return\_to] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to session[:return\_to] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:return\_to] = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to links\_path &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> 'new' &nbsp;&nbsp;&nbsp;end end
> 
> def destroy &nbsp;&nbsp;&nbsp;session[:user\_id] = @current\_user = nil end
> 
> I have surfed around the internet looking for solutions but their is no real consistent method out there. Everyone seems to have a different opinion on how to manage sessions/cookies. I am hoping to not have to change my controller structure (by say installing a plug-in) and sticking with Sessions creation.
> 
> Thank you in advance.

Michael, I would recommend using Authlogic because it has a method, remember\_me\_for, that one can redefine in the user\_session model to set the duration of time.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 4, 2010, 4:55pm UTC](https://discuss.rubyonrails.org/t/persistent-sessions/49755/3 "2010-03-04T16:55:09Z")

</div>

> Michael, I would recommend using Authlogic because it has a method, remember\_me\_for, that one can redefine in the user\_session model to set the duration of time.
> 
> Good luck,
> 
> -Conrad

I look into this but I am a bit concerned about whether there will be an overwriting or conflicts with my current models and controllers. Is is possible to install a plugin like Authlogic or restful\_authentication with a current user and sessions model and controller? I have already created a very basic user and session system and my users are tied in to my voting model so I am hoping to not have to rebuild any of my database.

If this is possible, do you know of a good tutorial for adding user management to an existing site with a more basic user management system?

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [March 4, 2010, 9:10pm UTC](https://discuss.rubyonrails.org/t/persistent-sessions/49755/4 "2010-03-04T21:10:14Z")

</div>

> > Michael, I would recommend using Authlogic because it has a method,
> 
> > remember\_me\_for, that one can redefine in the user\_session model to
> 
> > set the duration of time.
> 
> > 
> 
> > Good luck,
> 
> > 
> 
> > -Conrad

> I look into this but I am a bit concerned about whether there will be an
> 
> overwriting or conflicts with my current models and controllers. Is is
> 
> possible to install a plugin like Authlogic or restful\_authentication
> 
> with a current user and sessions model and controller? I have already
> 
> created a very basic user and session system and my users are tied in to
> 
> my voting model so I am hoping to not have to rebuild any of my
> 
> database.
> 
> If this is possible, do you know of a good tutorial for adding user
> 
> management to an existing site with a more basic user management system?

Michael, I would recommend reading the docs on the use of the gem which can be

found here:

[http://github.com/binarylogic/authlogic](http://github.com/binarylogic/authlogic)

Good luck,

-Conrad
