# If/else statement

**URL:** <https://www.ruby-forum.com/t/if-else-statement/107417>\
**Category:** Rails\
**Created:** [June 21, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/if-else-statement/107417 "2007-06-21T17:17:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gearoid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@gearoid](https://www.ruby-forum.com/u/gearoid)\
**Post date:** [June 21, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/if-else-statement/107417/1 "2007-06-21T17:17:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gearoid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@gearoid](https://www.ruby-forum.com/u/gearoid)\
**Post date:** [June 21, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/if-else-statement/107417/2 "2007-06-21T17:36:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gearoid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@gearoid](https://www.ruby-forum.com/u/gearoid)\
**Post date:** [June 21, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/if-else-statement/107417/3 "2007-06-21T17:43:54Z")

</div>

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
