Using the session to pass a value from form to controller

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

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]

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!

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