# Using the session to pass a value from form to controller

**URL:** <https://www.ruby-forum.com/t/using-the-session-to-pass-a-value-from-form-to-controller/123150>\
**Category:** Rails\
**Created:** [December 14, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/using-the-session-to-pass-a-value-from-form-to-controller/123150 "2007-12-14T12:49:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 14, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/using-the-session-to-pass-a-value-from-form-to-controller/123150/1 "2007-12-14T12:49:43Z")

</div>

I have a form in which a bunch of new users can be created with the same  
password if desired. I’m trying to write the password into the session  
and pull it out in the controller (besides avoiding putting it into  
params, this also should let me keep it for repopulating the text field  
if the creation fails for any reason and the user gets sent back to the  
page with the form).

As part of a form tag, i have this text field which is supposed to write  
the value entered in it into the session

\<% form\_tag :action =\> ‘batch\_save’, :id =\> @user do %\>  
…

Password:\<%= text\_field\_tag :password, session['new\_users\_password'] %\> .. \<%= submit\_tag 'Save' %\>

\<% end -%\>

But, in the batch\_save method in the controller, when i evaluate  
session[‘new\_users\_password’], i get nil back.

Can anyone see what i’m doing wrong?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 14, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/using-the-session-to-pass-a-value-from-form-to-controller/123150/2 "2007-12-14T12:55:07Z")

</div>

Password:\<%= text\_field\_tag :password, session['new\_users\_password']

will create a textfield in html output (the browser does not know  
anything about the rails session hash, anyway)  
this will be handed to your controller as every other form data in  
params[]

so params[:password] is the thing to use eg:  
session[:new\_user\_password] = params[:password]

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 14, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/using-the-session-to-pass-a-value-from-form-to-controller/123150/3 "2007-12-14T13:21:17Z")

</div>

Thorsten M. wrote:

> Password:\<%= text\_field\_tag :password, session['new\_users\_password']
> 
> will create a textfield in html output (the browser does not know  
> anything about the rails session hash, anyway)  
> this will be handed to your controller as every other form data in  
> params[]
> 
> so params[:password] is the thing to use eg:  
> session[:new\_user\_password] = params[:password]

ah. cool, i’ve written the value from params into the session in the  
controller and it’s fine now. thanks!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 14, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/using-the-session-to-pass-a-value-from-form-to-controller/123150/4 "2007-12-14T13:46:56Z")

</div>

On Dec 14, 2007, at 12:49 PM, Max W. wrote:

> As part of a form tag, i have this text field which is supposed to  
> write  
> the value entered in it into the session
> 
> \<% form\_tag :action =\> ‘batch\_save’, :id =\> @user do %\>  
> …
> 
> Password:\<%= text\_field\_tag :password, session['new\_users\_password']

Why would you need the session?

My interpretation of the feature is that you set as default the  
password of the last created user.

If that was the case, you are done passing params[:password] for that  
field no matter whether the creation of the user of that request is  
successful or not. If the creation is successful that’s clear. If not  
and the user edited the password and email validation failed you do  
not want to set the default password back, you want to let him correct  
the email and still set the password he chose, so params[:password]  
makes sense there as well.

– fxn
