If/else statement

Hi -

Having a problem with one of my if/else statements…

def back
if @session
redirect_to :action => ‘list’ <-- List can only be accessed if you’re
logged in.
else
redirect_to :action => ‘reference’ <-- Reference can be accessed at
any time
end
end

Problem is the code only seems to go as far as the if and never enters
the else… If you’re not logged in and click back it automatically
sends you to the login page which I don’t want, I want it to redirect to
the reference page.

I have a feeling that it is something small I’m leaving out but none the
less I can’t figure it out.
Thanks
Gearoid

Um, why are you checking for session? Unless you’ve manually turned it
off,
a session will always exist for any connection. Even then, that’s not
very
good practice. You really should be doing something like:

if session[:user]

else

end

And as a side note, I don’t know what version of Rails you’re using but
using the controller variables themselves (@session, @params, …) is
deprecated. You’re supposed to use the methods instead (session, params,
…).

Jason

Jason R. wrote:

Um, why are you checking for session? Unless you’ve manually turned it
off,
a session will always exist for any connection. Even then, that’s not
very
good practice. You really should be doing something like:

if session[:user]

else

end

And as a side note, I don’t know what version of Rails you’re using but
using the controller variables themselves (@session, @params, …) is
deprecated. You’re supposed to use the methods instead (session, params,
…).

Jason

Cheers Jason,

I’m new to this and still finding my feet, thanks for the advice.

Gearoid