I need help on creating these cookies for my Search options. I’ve read the Rails Guide and API but still don’t understand how to make cookies correctly. I have two checkboxes that filter search results. They start out as already checked but what I need it to do is save the settings in a cookie if one of them is unchecked for the next searches. Here is a picture for better understanding:

Answer:
class SearchController < ApplicationController
def index
@title = "Search"
@page_title = "Search"
# Checkboxes are already checked.
params[:online_search_checked] = true
params[:offline_search_checked] = true
restore_cookie # if user enters this page in the future we restore checkboxes state from the cookies. For the first visit checkbox value will be true.
end
def results
update_cookie # each time user submits search we need to update cookies.
restore_cookie # each time user submits search we need to show search page with correctly checked check boxes.
@title = "Search"
@page_title = "Search"
@search = Product.search do |q|
q.fulltext params[:search]
q.with(:coordinates, params[:coordinates]).near(latitude, longitude, :precision => 3) if params[:coordinates].present?
q.with(:online_search, false) if params[:online_search].nil? # search user prices that are online automatically if checkbox is checked.
q.with(:offline_search, true) if params[:offline_search].nil? # search user prices that are offline automatically if checkbox is checked.
q.paginate(:per_page => 20, :page => params[:page])
q.order_by(:purchase_date, :desc)
q.order_by(:price,:asc)
end
@products = @search.results
end
def update_cookie
update_cookie_with_param(:online_search, :online_search_checked)
update_cookie_with_param(:offline_search, :offline_search_checked)
end
def restore_cookie
restore_param_from_cookie(:online_search_checked)
restore_param_from_cookie(:offline_search_checked)
end
def update_cookie_with_param(value_param_name, checked_param_name)
checked = params[value_param_name].nil? ? "false" : "true"
cookies[checked_param_name] = { :value => checked, :expires => 2.weeks.from_now }
end
def restore_param_from_cookie(checked_param_name)
if cookies[checked_param_name]
params[checked_param_name] = (cookies[checked_param_name] == "true")
end
end
end
# On index and result page in partial
<%= form_tag results_search_index_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<%= label_tag :online_search, 'Online' %>
<%= check_box_tag :online_search, 'online_search_value', params[:online_search_checked] %>
<%= label_tag :offline_search, 'Offline' %>
<%= check_box_tag :offline_search, 'offline_search_value', params[:offline_search_checked] %>
<% end %>
In rails you can use cookies, cookies.permanent and cookies.signed to write cookies:
cookies– simple session cookiescookies.permanent– create cookieswith expiration date 20 years in the future
cookies.signed– generatesigned representation of cookie to pvervent tampering of its value by
the end user.
To explain cookies usage I created two session cookies for your application: ‘online_search_checked’ and ‘offline_search_checked’. They contain “true” or “false” values and represent whether appropriate checkbox is checked or not.
check_box_taghas following parameters: checkbox_name, checkbox_value, is_checked. We need to modify third parameter depending on the value received from the cookie. The second value may be a constant. If checkbox is checked, then params[:checkbox_name] == checkbox_value. If checkbox is unchecked, then params[:checkbox_name] == nil.Here is the algorithm:
‘online_search_checked’ and ‘offline_search_checked’ cookies set.
Then you would like to show him both check boxes checked by default.
we need to restore state of checkboxes from these cookies.
and restore state of checkboxes from these cookies.
Here is the code with my comments:
Hope this helps!
UPDATE
I do not quite understood what do you want to do here:
In your code
params[:online_search] == 1andparams[:offline_search] == 0expressions will always befalsebecauseparams[:online_search]andparams[:offline_search]arenilaccording toifcondition.