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