Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6921993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:55+00:00 2026-05-27T10:20:55+00:00

I need help on creating these cookies for my Search options. I’ve read the

  • 0

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:

How it looks


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 %>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:20:56+00:00Added an answer on May 27, 2026 at 10:20 am

    Cookie is a message given to a Web browser by a Web server. The
    browser stores the message in a text file. The message is then sent
    back to the server each time the browser requests a page from the
    server.

    In rails you can use cookies, cookies.permanent and cookies.signed to write cookies:

    • cookies – simple session cookies
    • cookies.permanent – create cookies
      with expiration date 20 years in the future
    • cookies.signed – generate
      signed 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_tag has 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:

    • The first time when user hits ‘search/index’ he does not have
      ‘online_search_checked’ and ‘offline_search_checked’ cookies set.
      Then you would like to show him both check boxes checked by default.
    • If this is not the first time then user already has cookies set and
      we need to restore state of checkboxes from these cookies.
    • When user hits ‘search/results’ we update cookies with the current state of checkboxes
      and restore state of checkboxes from these cookies.

    Here is the code with my comments:

    class SearchController < ApplicationController
      def index
        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
    
        # Using Sunspot here.
        @search = Product.search do |q|
          q.fulltext params[:search]
          q.with(:online_search, params[:online_search] == 1) if params[:online_search].nil?
          q.with(:offline_search, params[:offline_search] == 0) if params[:offline_search].nil?
        end
    
        @products = @search.results # Sunspot rendering 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', :method => 'get' do %>
       <%= text_field_tag :search, params[:search] %>
       <%= submit_tag "Search", :name => nil %>
       <%= check_box_tag :online_search, 'online_search_value', params[:online_search_checked] %>
       <%= check_box_tag :offline_search, 'offline_search_value', params[:offline_search_checked] %>
    <% end %>
    

    Hope this helps!


    UPDATE

    I do not quite understood what do you want to do here:

    q.with(:online_search, params[:online_search] == 1) if params[:online_search].nil?
    q.with(:offline_search, params[:offline_search] == 0) if params[:offline_search].nil?
    

    In your code params[:online_search] == 1 and params[:offline_search] == 0 expressions will always be false because params[:online_search] and params[:offline_search] are nil according to if condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a rails app which will help out businesses. These businesses have
I need help creating a regular expression for redirecting my old URL's to new
I need help with creating a C# method that returns the index of the
I need a little help creating a catch-all error handling page in my ICEfaces
i need some help with creating file Im trying in the last hours to
I just need help. Basically I am creating a windows appllication that sends bulk
need help/guide for sql select query, I have 2 table stock and stock_history, in
Need help in handling data and populating: I am creating Employee database, there are
I need some help figuring out the best way to proceed with creating a
I need help in creating my first application in MS Access 2007. Consider a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.