# Losing session variable with acts\_as\_authenticated

**URL:** <https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284>\
**Category:** rubyonrails-talk\
**Created:** [October 15, 2007, 2:57pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284 "2007-10-15T14:57:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![miss\_michelle](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@miss\_michelle](https://discuss.rubyonrails.org/u/miss_michelle)\
**Post date:** [October 15, 2007, 2:57pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/1 "2007-10-15T14:57:13Z")

</div>

Hi,

I initially set up acts\_as\_authenticated to require a user login before accessing and updating pages on my recipe site. This worked fine with no problems with the user session.

Now, I have changed the setup so that a few pages are available to the public, while others that involve create/update/delete actions require a user that is logged in. So I created a login form as a partial to include on every single page. The sign in form displays if not logged in; if the user is logged in it will display a welcome message to the user.

Now I'm encountering problems where my session[:user] variable gets reset to nil whenever I do a post request. I still have the session, but I just lose the variable, and find myself having to explicitly send the user ID as hidden input and re-assign the session[:user] variable. Has anyone encountered this problem, or have any idea why this would be happening?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![M\_Graff](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@M\_Graff](https://discuss.rubyonrails.org/u/M_Graff)\
**Post date:** [October 15, 2007, 10:48pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/2 "2007-10-15T22:48:07Z")

</div>

I am using exactly this trick on my [http://eq2guild.flame.org/](http://eq2guild.flame.org/) site, and have experienced no problems with it.

Can you compare the html code on that opening page with what you have in yours? I can send you my controller snipit if it will help.

--Michael

---

<div class="post-metadata">

**Author:** ![miss\_michelle](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@miss\_michelle](https://discuss.rubyonrails.org/u/miss_michelle)\
**Post date:** [October 16, 2007, 5:50pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/3 "2007-10-16T17:50:40Z")

</div>

Hi Michael,

Thanks for your reply. Our login forms are pretty much the same except that your form calls on the /account/login action, whereas mine calls on /recipe (I had put my login action in my application controller so that it would be accessible to all my controllers).

This is how I generated it in my view: \<% form\_for :login, login do %\>

It works on my /recipe page, but I just recently noticed that I can't log in

Aside from that issue, it would be great if I could take a look at your controller code. To me, it doesn't really seem like any of my update/edit actions for recipes explicitly change any session variables (except when I have to re-assign my session[:user] variable to keep the user logged in).

Michelle

Michael Graff-5 wrote:

---

<div class="post-metadata">

**Author:** ![M\_Graff](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@M\_Graff](https://discuss.rubyonrails.org/u/M_Graff)\
**Post date:** [October 16, 2007, 6:00pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/4 "2007-10-16T18:00:44Z")

</div>

Wait, you have your login method in all your controllers? May I ask why? 🙂

It seems that having one definite action to log one in is a very good thing. That is, /login or /account/login or /log\_me\_in should be the only controller/action with a login method. After all, you don't log into a recipe, do you? 🙂

If you require a user to log in before they do certain operations, that's also easily done:

class ToonController \< ApplicationController &nbsp;&nbsp;before\_filter :login\_required, :except =\> [:list, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:show, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:show\_avatar, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:classes, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:tradeskills]

Or, in each method:

&nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;if !logged\_in? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'You cannot create characters without logging in.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> "list" &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@toon = Toon.new &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

My account controller:

class AccountController \< ApplicationController &nbsp;&nbsp;def login &nbsp;&nbsp;&nbsp;&nbsp;return unless request.post? &nbsp;&nbsp;&nbsp;&nbsp;self.current\_user = User.authenticate(params[:login], params[:password]) &nbsp;&nbsp;&nbsp;&nbsp;if logged\_in? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_back\_or\_default(:controller =\> 'welcome', :action =\> 'index') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Logged in successfully" &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def logout &nbsp;&nbsp;&nbsp;&nbsp;cookies.delete :auth\_token &nbsp;&nbsp;&nbsp;&nbsp;reset\_session &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "You have been logged out." &nbsp;&nbsp;&nbsp;&nbsp;redirect\_back\_or\_default(:controller =\> 'welcome', :action =\> 'index') &nbsp;&nbsp;end

As I mentioned, I'm using a modified acts\_as\_authenticated plugin.

--Michael

---

<div class="post-metadata">

**Author:** ![miss\_michelle](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@miss\_michelle](https://discuss.rubyonrails.org/u/miss_michelle)\
**Post date:** [October 16, 2007, 8:37pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/5 "2007-10-16T20:37:32Z")

</div>

Sorry, what I mean is that I had put my login method inside my application controller so it's accessible to the rest of my controllers. If I leave my login method in the account controller, my other controllers throw an exception with the error "undefined method login".

My login and logout methods otherwise look similar to yours (they're unmodified from acts\_as\_authenticated).

This is how I'm rendering the login box in the main layout:

\<%= render :partial =\> 'account/login', :locals =\> { :current\_user =\> @current\_user } %\>

\_login.rhtml (I just changed it so that login is now back in the account controller):

\<% if flash[:notice] %\> &nbsp;&nbsp;\<%= flash[:notice] %\> \<% end %\>

\<% if logged\_in? %\> &nbsp;&nbsp;\<%= 'Welcome, ' + current\_user.first\_name + "! &raquo; " %\> &nbsp;&nbsp;\<%= link\_to 'Logout', :controller =\> 'account', :action =\> 'logout' %\> \<% else %\> &nbsp;&nbsp;\<% form\_tag "/account/login" method="post" %\> &nbsp;&nbsp;\<p\>\<label for="login"\>Login\</label\>\<br/\> &nbsp;&nbsp;\<%= text\_field\_tag 'login', login %\>\</p\>

&nbsp;&nbsp;\<p\>\<label for="password"\>Password\</label\>\<br/\> &nbsp;&nbsp;\<%= password\_field\_tag 'password' %\>\</p\> &nbsp;&nbsp; &nbsp;&nbsp;\<p\>\<label for="remember\_me"\>Remember me:\</label\> &nbsp;&nbsp;\<%= check\_box\_tag 'remember\_me' %\>\</p\> &nbsp;&nbsp; &nbsp;&nbsp;\<p\>\<%= submit\_tag 'Log in' %\>\</p\> &nbsp;&nbsp;\<% end %\> \<% end %\>

Michael Graff-5 wrote:

---

<div class="post-metadata">

**Author:** ![M\_Graff](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@M\_Graff](https://discuss.rubyonrails.org/u/M_Graff)\
**Post date:** [October 16, 2007, 8:50pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/6 "2007-10-16T20:50:51Z")

</div>

> Sorry, what I mean is that I had put my login method inside my application controller so it's accessible to the rest of my controllers. If I leave my login method in the account controller, my other controllers throw an exception with the error "undefined method login".

You should never have "login" run via the path /recipe/login, or /book/login, etc. probably -- you always want /account/login -- so login should be in account\_controller.rb only. If you put it in app/controllers/application.rb, I believe it can be accessed via /recipe/login, etc.

If you need to refer to it inside a different controller, you can always redirect to the login page:

&nbsp;&nbsp;redirect\_to :controller =\> :account, :action =\> :login

> This is how I'm rendering the login box in the main layout:

What you did looks basically the same as mine...

Here's my app/views/account/login.rhtml file:

\<div class="contentbody"\> \<% form\_tag do -%\> \<p\>\<label for="login"\>Login\</label\>\<br/\> \<%= text\_field\_tag 'login' %\>\</p\> \<p\>\<label for="password"\>Password\</label\>\<br/\> \<%= password\_field\_tag 'password' %\>\</p\> \<p\>\<%= submit\_tag 'Log in' %\>\</p\> \<% end -%\> \</div\>\<!-- contentbody --\>

This renders to:

\<div class="contentbody"\> \<form action="/account/login" method="post"\>\<p\>\<label for="login"\>Login\</label\>\<br/\> \<input id="login" name="login" type="text" /\>\</p\> \<p\>\<label for="password"\>Password\</label\>\<br/\> \<input id="password" name="password" type="password" /\>\</p\> \<p\>\<input name="commit" type="submit" value="Log in" /\>\</p\> \</form\>\</div\>\<!-- contentbody --\>

---

<div class="post-metadata">

**Author:** ![miss\_michelle](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@miss\_michelle](https://discuss.rubyonrails.org/u/miss_michelle)\
**Post date:** [October 16, 2007, 9:15pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/7 "2007-10-16T21:15:50Z")

</div>

I'm wondering why you're login form is in a login.rhtml rather than a partial named \_login.rhtml? When I try the \<% form\_tag do -%\> it renders the action to go to the page that I'm current on, eg. /recipe/list.

Basically, I'm trying to get the layout to be just like yours: login form included onto each page as a partial. But when I put the login method back into the application, my recipe controller is complaining about an undefined login method. Incidentally, trying to access the path /recipe/login results in an error because I have no layout page for that.

Michael Graff-5 wrote:

---

<div class="post-metadata">

**Author:** ![M\_Graff](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@M\_Graff](https://discuss.rubyonrails.org/u/M_Graff)\
**Post date:** [October 16, 2007, 11:51pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/8 "2007-10-16T23:51:48Z")

</div>

My bad. I have two login forms actually -- that one is displayed when the user goes to /admin/login specifically.

Here is the one I put in my sidebars, which appears on every page unless the user is logged in. And no I don't have this in a partial either, but I probably should...

\<div id="login\_form"\> &nbsp;&nbsp;\<% form\_tag(:controller =\> "account", :action =\> "login") do %\> &nbsp;&nbsp;&nbsp;&nbsp;\<label for="login" class="lfield"\>Username\</label\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= text\_field\_tag(:login, params[:login], :size =\> 12, :class =\> "lfield") %\> &nbsp;&nbsp;&nbsp;&nbsp;\<p/\> &nbsp;&nbsp;&nbsp;&nbsp;\<label for="password" class="lfield"\>Password\</label\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= password\_field\_tag(:password, params[:password], :size =\> 12, :class =\> "lfield") %\> &nbsp;&nbsp;&nbsp;&nbsp;\<p/\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag "Log In", :class =\> "submit\_button" %\> &nbsp;&nbsp;\<% end %\> \</div\>\<!-- login\_form --\>

This renders into:

\<div id="login\_form"\> &nbsp;&nbsp;&nbsp;\<form action="/account/login" method="post"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<label for="login" class="lfield"\>Username\</label\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<input class="lfield" id="login" name="login" size="12" type="text" /\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p/\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<label for="password" class="lfield"\>Password\</label\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<input class="lfield" id="password" name="password" size="12" type="password" /\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p/\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<input class="submit\_button" name="commit" type="submit" value="Log In" /\> &nbsp;&nbsp;&nbsp;&nbsp;\</form\> \</div\>\<!-- login\_form --\>

---

<div class="post-metadata">

**Author:** ![miss\_michelle](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@miss\_michelle](https://discuss.rubyonrails.org/u/miss_michelle)\
**Post date:** [October 17, 2007, 7:38pm UTC](https://discuss.rubyonrails.org/t/losing-session-variable-with-acts-as-authenticated/19284/9 "2007-10-17T19:38:49Z")

</div>

It turns out that I had been requiring the login method in a before\_filter, which is why my application was breaking when I moved the login method back to the account controller. It works now... I'm able to log in from different pages, and it also fixed my session problem 🙂

Thanks so much for your help!

Michael Graff-5 wrote:
