# Pulling information out of the session in the view

**URL:** <https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373>\
**Category:** Rails\
**Created:** [November 5, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373 "2006-11-05T20:59:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 5, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373/1 "2006-11-05T20:59:34Z")

</div>

Hi, I’m setting up a basic user authentication system and I don’t know  
how to use the information that gets loaded into the session once a user  
logs in. The session is session[:user], which I believe contains all of  
the user information. I created a little shortcut for session[:user]  
like this:

def current\_user  
session[:user]  
end

in my application.rb.

How do I call individual pieces of information out of that in my view?  
Like if I wanted to display the current user’s username somewhere?

Let me know if more information is needed, but I assume this is kind of  
a generic thing.

Thanks so much!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 5, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373/2 "2006-11-05T21:32:48Z")

</div>

Dave A. wrote:

> Hi, I’m setting up a basic user authentication system and I don’t know  
> how to use the information that gets loaded into the session once a user  
> logs in. The session is session[:user], which I believe contains all of  
> the user information. I created a little shortcut for session[:user]  
> like this:
> 
> def current\_user  
> session[:user]  
> end
> 
> in my application.rb.
> 
> How do I call individual pieces of information out of that in my view?  
> Like if I wanted to display the current user’s username somewhere?
> 
> Let me know if more information is needed, but I assume this is kind of  
> a generic thing.
> 
> Thanks so much!

In a view you can just do:

\<%= session[:user].username %\>

And if you add to your application.rb that has the current\_user method  
in it:

helper\_method :current\_user

Then your current\_user method will be available in the view:

\<%= current\_user.username %\>

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 5, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373/3 "2006-11-05T22:41:01Z")

</div>

Thanks, it works great. I knew it was something simple like that. I  
have a follow-up, though. I’d like to insert current\_user.login (the  
name of the user) into a form when a user creates a recipe (it’s recipe  
site I’m building). Here’s what I have now, but it doesn’t work:

\<%= text\_field ‘recipe’, ‘username’, current\_user.login %\>

Also, how do I make the text field hidden to the user, so they don’t  
even see that it’s being recorded under their name; it just does it  
automatically?

Once again, I’m sure this is simple, but this is my first time doing any  
sort of serious programming or web development, so little help like this  
goes a long way for me. 🙂

Thanks again!!

Dave

Alex W. wrote:

> Dave A. wrote:
> 
> > Hi, I’m setting up a basic user authentication system and I don’t know  
> > how to use the information that gets loaded into the session once a user  
> > logs in. The session is session[:user], which I believe contains all of  
> > the user information. I created a little shortcut for session[:user]  
> > like this:
> > 
> > def current\_user  
> > session[:user]  
> > end
> > 
> > in my application.rb.
> > 
> > How do I call individual pieces of information out of that in my view?  
> > Like if I wanted to display the current user’s username somewhere?
> > 
> > Let me know if more information is needed, but I assume this is kind of  
> > a generic thing.
> > 
> > Thanks so much!
> 
> In a view you can just do:
> 
> \<%= session[:user].username %\>
> 
> And if you add to your application.rb that has the current\_user method  
> in it:
> 
> helper\_method :current\_user
> 
> Then your current\_user method will be available in the view:
> 
> \<%= current\_user.username %\>

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 6, 2006, 2:15am UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373/4 "2006-11-06T02:15:16Z")

</div>

Nevermind, I answered my own question:

\<%= hidden\_field ‘recipe’, ‘username’, :value =\> current\_user.login %\>

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 6, 2006, 6:18am UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373/5 "2006-11-06T06:18:29Z")

</div>

Dave A. wrote:

> Nevermind, I answered my own question:
> 
> \<%= hidden\_field ‘recipe’, ‘username’, :value =\> current\_user.login %\>

A better way would be to do this on your new Recipe object in the  
controller of the “new” action.

def new  
@recipe = Recipe.new(:username =\> current\_user.login)  
end

Now any form helper will automatically have this as its value, because  
the object its references has that value:

\<%= hidden\_field ‘recipe’, ‘username’ %\>

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/pulling-information-out-of-the-session-in-the-view/74373/6 "2006-11-07T11:06:11Z")

</div>

well i would suggest not to use a hidden field here …  
you can always identify the user adding a recipe though his session, as  
shown in your first question.  
so why add the name to the form?

1. It’s prone for abuse by manipulating the sent username in the POST  
arguments of the request  
(not such a big deal in a recipe app i guess, but bad nonetheless
2. to prevent this, you have to check if the sent username is the  
username of the user currently logged in.  
so why add it to the form at all?

@recipe = Recipe.new(params[:recipe]  
@recipe.user\_id = current\_user.id  
@recipe.username = current\_user.login  
@recipe.save

- i added the user’s id as it should be added for Model Relationships
- adding the username was therefore useless but i did it nonetheless.
