SkRoR
SkRoR

Reputation: 1199

How to store location value as cookie in ruby on rails

I need to store the selected location value as a cookie value.

I have a Location and Property table , I fetches and displays all locations from location table, so when a user clicks on this location it should search all properties related to that location.

Search is happening now, but problem is how to store the selected location value as a cookie value.

my layout page view.

<%= form_tag location_search_path, :method=>'get' do %> 
<%= select_tag :location_id, options_from_collection_for_select(Location.all, :id, :name, params[:q]), :class=>"btn btn-primary dropdown-toggle" ,:onchange=>'this.form.submit()'%><br>
<% end %>

my search controller.

def location_search
    location=params[:location_id]
    @location = Location.find(location).name if params[:location_id].present?
    @property = Property.where(['location LIKE ?  AND status=?', "%#{@location}%", '3']).all
end

Please help.Any help is appreciatable.

Upvotes: 1

Views: 2269

Answers (1)

Kuba Płoskonka
Kuba Płoskonka

Reputation: 177

You can just set it by using cookies[:cookie_name] = value. So in your case you probably want something like this in location_search method in you controller:

def location_search
  location=params[:location_id]
  @location = Location.find(location).name if params[:location_id].present?
  @property = Property.where(['location LIKE ?  AND status=?', "%#{@location}%", '3']).all

  cookies[:location] = @location
end

You can read more about Cookies in rails here: http://guides.rubyonrails.org/action_controller_overview.html#cookies

Upvotes: 3

Related Questions