# Pass session value in database field

**URL:** <https://www.ruby-forum.com/t/pass-session-value-in-database-field/175647>\
**Category:** Rails\
**Created:** [September 17, 2009, 3:16am UTC](https://www.ruby-forum.com/t/pass-session-value-in-database-field/175647 "2009-09-17T03:16:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 17, 2009, 3:16am UTC](https://www.ruby-forum.com/t/pass-session-value-in-database-field/175647/1 "2009-09-17T03:16:11Z")

</div>

Hello -

Noobie question I’m sure…

I’m trying to pass a session value to a database field ‘user’ when the  
user creates a new blog entry. The session value is created when the  
user logs on to the site. I have this in the view to create a new  
blog entry:

\<%= f.text\_field :user, :value =\> session[:name], :disabled =\> true  
%\>

but it fails validation when I create the entry  
(validates\_presence\_of :user). Is there a way for me to store the  
session value into my database field ‘user’ when the user creates the  
new entry (and prevent the user from manipulating the value)?

Many thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 17, 2009, 3:17am UTC](https://www.ruby-forum.com/t/pass-session-value-in-database-field/175647/2 "2009-09-17T03:17:57Z")

</div>

On Wed, Sep 16, 2009 at 7:51 PM, Mark [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way for me to store the  
> session value into my database field ‘user’ when the user creates the  
> new entry (and prevent the user from manipulating the value)?

You can add stuff to the incoming “params” hash using merge.

@user = User.find( session[:user\_id] )

params[:blog].merge( :user =\> @user ) if @user

Blog.create!( params[:blog] )

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 17, 2009, 3:22am UTC](https://www.ruby-forum.com/t/pass-session-value-in-database-field/175647/3 "2009-09-17T03:22:38Z")

</div>

On Sep 17, 1:51 am, Mark [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> %\>

> but it fails validation when I create the entry  
> (validates\_presence\_of :user). Is there a way for me to store the

Disabled form elements are not submitted by the browser. Even if this  
did work it would only stop the most casual of users form editing the  
submitted data.  
Why not do (in your controller’s create method) something.name =  
session[:name] ?

Fred

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 18, 2009, 6:22am UTC](https://www.ruby-forum.com/t/pass-session-value-in-database-field/175647/4 "2009-09-18T06:22:58Z")

</div>

That did it. Thanks!

On Sep 16, 8:08 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
