0

How do I save a session variable (maybe into a cookie) when a user inputs a location? I'd like to keep this information for future sign ins. So they won't have to retype their location multiple times.

<%= form_tag search_path, :method => :get do %>
  <%= text_field_tag :q, params[:q], :class => "search" %>
  <%= text_field_tag :loc, params[:loc], :class => "search-main", :id => "loc-search", :placeholder => "address, neighborhood, city, state or zip" %>
  <%= button_tag :type => :submit, :class => "btn btn-medium btn-inverse" do %>
    <i class="icon-search icon-white"></i>
  <% end %>
<% end %>

This is in my layouts/application.html.erb

I'd have to set a variable if it hasn't been set yet, and display it in the field if it is already set

2
  • Why not just add a field location to user's model and keep it there? Commented Dec 17, 2013 at 6:45
  • @YevgeniyAnfilofyev do you mean saving it into the database? Commented Dec 17, 2013 at 7:13

2 Answers 2

2

You can use cookies:

cookies[:name] = {
  value: 'my_cookie',
  expires: 1.year.from_now,
}

See more here.

You can also use session_store like this.

Sign up to request clarification or add additional context in comments.

Comments

1

have you tried:

session[:location] = params[:loc]

2 Comments

everytime i close the browser, the session disappears. I've also tried cookies[:location]. Is it supposed to disappear?
@andrewliu yes, that is by design. Rails CookieStore uses a session cookie; session cookies are only valid until the browser is closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.