Textbox to session

Can anyone help me out with storing a user’s location to session to be
used several times by a user during a login ? I need to create a
textbox where the user enters there zip code and then I need to hold
that zip code value in session to be used for several queries
throughout the user’s time within the site. I also would like to
return the user’s actual town and state after the zip is submitted but
that is of lesser importance. I think I almost have the controller
code but not quite there yet

def store_location
session [:location] = params[:location]
end

I think its close although I am new and not totally sure what it is
doing. The view code I don’t even know how to start though and I
assume it is only the controller and the ‘store location’ view that I
need to code for?

Thanks

The code you have there will do what you expect – it will store
exactly what is on the form submitted to that controller into the
session.

–Michael

thank you
what does the view look like though - I am weakest on the view side
right now, horrible at html,css…

your the best - thanks a ton

I’ll paste a simple controller and an associated view (which includes
a form). That might help.

Here is my login form, in app/views/session/new.rhtml

<% form_tag(:controller => "session", :action => "create") do -%>
Login
<%= text_field_tag 'login' %>
Password
<%= password_field_tag 'password' %>
<%= submit_tag 'Log in' %>
<% end -%>

And here is the part of my controller which handles it:

class SessionController < ApplicationController
def new
end

def create
if using_open_id?
open_id_authentication(params[:openid_url])
else
password_authentication(params[:login], params[:password])
end
end

def destroy
# self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
flash[:notice] = “You have been logged out.”
redirect_back_or_default ‘/’
end

def password_authentication(login, password)
self.current_user = User.authenticate(login, password)
if logged_in?
successful_login
else
failed_login
end
end

def failed_login(message = “Authentication failed.”)
flash.now[:error] = message
render :action => ‘new’
end

def successful_login
redirect_back_or_default(’/’)
flash[:notice] = “Logged in successfully.”
end
end

“Your location is #{params[:location]}” would work.

Note that it is subject to XSS attacks if it is displayed to 3rd
parties. You’d need to escape the HTML in it if it was.

–Michael

it works thanks a ton!!!
i had my quotes messed up - still new

OK I think I have the view working, textbox is there
here is my method in the controller:

def store_location
session[:location] = params[:location]
redirect_to :action => ‘list’
flash[:notice] = "your location is "
end

so right now you enter a zipcode and it should be storing it to the
session, the redirect keeps me on the same page and the flash notice
seems to work as well. What I would like to do now is add to that
flash notice the actual value I just stored in the hash…something
like …flash[:notice] = "your location is " + [session value]

can anyone help with the syntax on this?

BTW does that session code look correct for actualy getting the zip
stored in the session? I have not been able to verify yet becasue I
cant get the flash to return it.

here is my view code just in case you need it…

<%= start_form_tag :action => ‘store_location’%>
<%= text_field ‘location’, nil %>
<%= submit_tag ‘your location’ %>
<%= end_form_tag %>

look ok?

JR