# Textbox to session

**URL:** <https://www.ruby-forum.com/t/textbox-to-session/118608>\
**Category:** Rails\
**Created:** [October 29, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/textbox-to-session/118608 "2007-10-29T14:03:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 29, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/1 "2007-10-29T14:03:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 29, 2007, 2:14pm UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/2 "2007-10-29T14:14:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 29, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/3 "2007-10-29T16:20:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 29, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/4 "2007-10-29T19:40:22Z")

</div>

your the best - thanks a ton

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 29, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/5 "2007-10-29T18:48:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 30, 2007, 3:52am UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/6 "2007-10-30T03:52:44Z")

</div>

“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

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 30, 2007, 4:00am UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/7 "2007-10-30T04:00:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agilehack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@agilehack](https://www.ruby-forum.com/u/agilehack)\
**Post date:** [October 30, 2007, 3:46am UTC](https://www.ruby-forum.com/t/textbox-to-session/118608/8 "2007-10-30T03:46:53Z")

</div>

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
