# Need very simple help

**URL:** <https://www.ruby-forum.com/t/need-very-simple-help/185493>\
**Category:** Rails\
**Created:** [March 21, 2010, 4:25pm UTC](https://www.ruby-forum.com/t/need-very-simple-help/185493 "2010-03-21T16:25:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 4:25pm UTC](https://www.ruby-forum.com/t/need-very-simple-help/185493/1 "2010-03-21T16:25:02Z")

</div>

I have several questions and i need clear understanding on how i should  
approach them, my target is for creating something called “fakebook”,  
facebooks copy but not going live at all (uni project).

Security Question  
This is the code that helps me protect my “user/index”…

private  
def protect  
unless session[:user\_id]  
flash[:notice] = “Please Login to view this page, or Register if  
you do not have an account!”  
redirect\_to :action =\> :login  
return false  
end  
end

It works perfectly, as it checks for the session ID and when it finds  
the user is logged in, he can view the user/index (another code is  
placed on top with this).  
Otherwise, they cannot view it and it takes them to the login page and a  
flash message is displayed.

When the user is logged in, i want it to redirect the “user/register”  
and “user/login” to the “user/index”, and if they are NOT logged in,  
they can view them. I have done this with the IF command in my menu but  
physically they can type the address aka user/register or user/login and  
view them again.

How can i redirect this?

thank you for your help!

-Guru

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/need-very-simple-help/185493/2 "2010-03-21T16:45:42Z")

</div>

On Sun, Mar 21, 2010 at 12:25 PM, Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> flash[:notice] = "Please Login to view this page, or Register if
> 
> ```
> 
> flash message is displayed.

I’d strongly suggest you to take a look at Authlogic[1] gem. Even when  
this is a very simple/personal project, and implement this gem might  
look too much overhead for you, you’ll get very interesting concepts  
for authentication.

[1][GitHub - binarylogic/authlogic: A simple ruby authentication solution.](http://github.com/binarylogic/authlogic)

Hope it helps.

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/need-very-simple-help/185493/3 "2010-03-21T16:56:12Z")

</div>

Leonardo M. wrote:

> On Sun, Mar 21, 2010 at 12:25 PM, Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ï¿½ ï¿½ ï¿½flash[:notice] = "Please Login to view this page, or Register if  
> > flash message is displayed.
> 
> I’d strongly suggest you to take a look at Authlogic[1] gem. Even when  
> this is a very simple/personal project, and implement this gem might  
> look too much overhead for you, you’ll get very interesting concepts  
> for authentication.
> 
> [1][GitHub - binarylogic/authlogic: A simple ruby authentication solution.](http://github.com/binarylogic/authlogic)
> 
> Hope it helps.
> 
> –  
> Leonardo M…  
> There’s no place like ~

Thank you for your reply, i am a very basic student and i need to take  
it one bit a time. This was small homework set, ill have to take it  
small steps before i can leap.

Would love a simple answer so i can understand it.

Thanks once again,

Jagmit G.

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 6:58pm UTC](https://www.ruby-forum.com/t/need-very-simple-help/185493/4 "2010-03-21T18:58:42Z")

</div>

Colin L. wrote:

> On 21 March 2010 15:25, Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ ï¿½flash[:notice] = "Please Login to view this page, or Register if  
> > flash message is displayed.
> > 
> > When the user is logged in, i want it to redirect the “user/register”  
> > and “user/login” to the “user/index”, and if they are NOT logged in,  
> > they can view them. I have done this with the IF command in my menu but  
> > physically they can type the address aka user/register or user/login and  
> > view them again.
> 
> You have already shown us how you redirect from index to login if the  
> user is not logged in. You are asking how to redirect from register  
> and login if not logged in. Just use basically the same technique but  
> the test will be the the other way round (‘if’ instead of ‘unless’).
> 
> Colin

Thank you very much Colin,

Simple code:

if session[:user\_id]  
flash[:notice] = “Already registed and logged in! You cannot view  
the register or login!”  
redirect\_to :action =\> :index  
end  
end

Thank you for making me understand 😃

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/need-very-simple-help/185493/5 "2010-03-21T17:47:11Z")

</div>

On 21 March 2010 15:25, Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> flash[:notice] = "Please Login to view this page, or Register if
> 
> ```
> 
> flash message is displayed.
> 
> When the user is logged in, i want it to redirect the “user/register”  
> and “user/login” to the “user/index”, and if they are NOT logged in,  
> they can view them. I have done this with the IF command in my menu but  
> physically they can type the address aka user/register or user/login and  
> view them again.

You have already shown us how you redirect from index to login if the  
user is not logged in. You are asking how to redirect from register  
and login if not logged in. Just use basically the same technique but  
the test will be the the other way round (‘if’ instead of ‘unless’).

Colin
