Friday, 30 August 2013

Is it possible to update a variable using a form in Rails?

Is it possible to update a variable using a form in Rails?

I'm working on a form that should allow users to update a session variable
called session[:city] that tracks their location. The variable doesn't
need to be saved in the database cause it's kind of a throwaway; it only
needs to exist while the user is on the site.
session[:city] is used in several places throughout the site, so I've
placed the following method in the Application Controller:
class ApplicationController < ActionController::Base
before_filter :set_city
def set_city
unless session[:city].present?
session[:city] = request.location.city
end
end
end
That part works correctly, and I'm able to call the variable throughout
the site.
The issue I'm having is in updating the variable by a form. The only
action I need it to do is update the variable, but it's not responding. I
know I'm missing something here, but after trying a bunch of things I'm a
bit stumped. This is my basic form currently:
<%= form_tag do %>
<%= text_field_tag session[:city] %>
<%= submit_tag %>
<% end %>

No comments:

Post a Comment