# Newbie question on usersession

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-usersession/86395>\
**Category:** Rails\
**Created:** [February 21, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/newbie-question-on-usersession/86395 "2007-02-21T23:46:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@madmax](https://www.ruby-forum.com/u/madmax)\
**Post date:** [February 21, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/newbie-question-on-usersession/86395/1 "2007-02-21T23:46:02Z")

</div>

all:  
prob a stupid question… new to ruby/rails.

I have a working ruby on rails app on my server. However, i don’t  
provide authentication service. (login etc)… In my workplace, there  
is a centrally authenticated URL- if ppl go there first, they can get  
authenticated. i thought i will put my html start page in this  
centrally authenticated place(with a form and hidden param and  
redirect to my ruby on rails server).

I however, want to prevent ppl directly using [http://myServer/app/main](http://myServer/app/main)

So,  
in my main method, i do this  
def controller\_main  
@user = @params[‘user’]  
if (@user == nil)  
redirect\_to “[http://goway.com](http://goway.com)” # basically send them to some no  
accesspl  
end  
end

This looked good… but the problem is: i have other methods like list  
def list

# This lists all my data

#here my check for @user always returns nil. Why?  
end

Only main method gets access to the html hidden param ‘user’. i dont  
know why storing it in @user does not work (i thought its instance  
variable).

---

<div class="post-metadata">

**Author:** ![madmax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@madmax](https://www.ruby-forum.com/u/madmax)\
**Post date:** [February 21, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/newbie-question-on-usersession/86395/2 "2007-02-21T23:59:49Z")

</div>

madmax wrote:

> all:  
> prob a stupid question… new to ruby/rails.
> 
> I however, want to prevent ppl directly using [http://myServer/app/main](http://myServer/app/main)
> 
> So,  
> in my main method, i do this  
> def controller\_main  
> @user = @params[‘user’]  
> if (@user == nil)  
> redirect\_to “[http://goway.com](http://goway.com)” # basically send them to some no  
> accesspl  
> end  
> end
> 
> This looked good… but the problem is: i have other methods like list  
> def list
> 
> # This lists all my data
> 
> #here my check for @user always returns nil. Why?  
> end

You need a before\_filter in you application.rb to ensure that this @user  
object gets created on every request. Instance variables in the  
controller only exist for the length of one request, and then die.

#application.rb  
class ApplicationController \< ActionController::Base  
before\_filter :authenticate  
def authenticate  
@user = @params[‘user’]  
redirect\_to login\_url unless @user  
end  
end

---

<div class="post-metadata">

**Author:** ![madmax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@madmax](https://www.ruby-forum.com/u/madmax)\
**Post date:** [February 22, 2007, 3:07am UTC](https://www.ruby-forum.com/t/newbie-question-on-usersession/86395/3 "2007-02-22T03:07:21Z")

</div>

## Thanks for your reply. It still does not work for me. It only works on access to my main page (since the html redirect is passing a hidden param). in the login server, my html page content is like this

---------------------

once my main page is loaded - [http://myServer/app/main](http://myServer/app/main) , in that  
main.rhtml, i have link defined like below.  
\<%= link\_to “(Show all Books)”,  
:controller =\> “app”,  
:action =\> “list” %\>

now, clicking on link, redirects me to login page (even though i did  
come from that)…  
so the authenticate method stores the @user = @params[‘user’]… but it  
also seems to lose it

---

<div class="post-metadata">

**Author:** ![madmax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@madmax](https://www.ruby-forum.com/u/madmax)\
**Post date:** [February 25, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/newbie-question-on-usersession/86395/4 "2007-02-25T17:55:17Z")

</div>

the way i solved is using session[:user] = @params[‘user’]

and in other methods i just use the value of session[:user]
