# Getting value from session - syntax help

**URL:** <https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814>\
**Category:** Rails\
**Created:** [March 13, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814 "2008-03-13T14:32:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 13, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/1 "2008-03-13T14:32:09Z")

</div>

Hi, I’m trying to make a rails app and although I understand the logic  
of what I’m trying to do, the syntax used in ruby really confuses me as  
it seems to do a lot of the work automatically.

I have 3 tables, user, forum and post. They are related as follows:  
User  
has\_many :posts  
end

Forum  
has\_many :posts  
end

Post  
belongs\_to :forum  
belongs\_to :user  
end

When a user registers or logs in, the user is stored in a session. The  
user can then add a post to a form by using the default form generated  
by the scaffold. However, atm, the user can type in any user ID into  
the form. I would like to delete this part of the form and use the  
current [:user][:user\_id] in it’s place but I don’t know how to combine  
this with the already existing params in the controller.  
Any advice would be appreciated, thanks in advance.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 13, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/2 "2008-03-13T15:55:23Z")

</div>

so you have something like this for login:

after finding user with name/pwd @user=User.find(…)  
session[:user\_id] = @user.id

and all following will just do:  
@current\_user = User.find(session[:user\_id])

best to be placed in a before\_filter

and access usert data like  
@current\_user.posts

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 13, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/3 "2008-03-13T17:48:29Z")

</div>

I followed an online tutorial for my user login.

I have the following method in my application controller

def current\_user  
session[:user]  
end

Then before\_filter :current\_user in my post controller…

I still don’t see how I can get the id and use it, sorry, the ruby  
syntax just makes no sense to me 😕

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 13, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/4 "2008-03-13T17:59:20Z")

</div>

ok, this makes sense. most people store only the id in the session. in  
your case it seems to store the whole user object. but that’s ok.

after finding user with name/pwd @user=User.find(…)  
session[:user] = @user

this function simply returns this object (in Ruby you don’t need an  
explicit return statement, whatever is in the last line will be  
returned, session[:user] in that case)  
def current\_user  
session[:user]  
end

current\_user will give you the whole user and you can get the id:  
current\_user.id

and access user data like  
current\_user.posts

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 14, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/5 "2008-03-14T20:44:41Z")

</div>

Hi again. Sorry to be a pain! I can access and display the values  
fine, but how can I combine the value with the values obtained by the  
form?  
For example, if I have a variable

@currentid = current\_user.id

How can I add it to the params in the create method of the controller?

def create  
@post = @forum.posts.build(params[:post])

I apologise for being slow, but rails confuses me as it seems to do a  
lot of the work for you, which is great if you understand it but bizzare  
when you are trying to analyze things.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 14, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/6 "2008-03-14T20:53:18Z")

</div>

i don’t understand exactly, what you want, but in general:  
params is a hash  
to add something just do:  
params[:key] = value

for a form you have a hash within a hash so you can do:  
params[:post][:key] = value

so if i get it right you want to do something like  
params[:post][:user\_id] = @currentid

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [March 14, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/getting-value-from-session-syntax-help/131814/7 "2008-03-14T21:27:54Z")

</div>

Thanks! I’ve got it working now:)
