为搜索选项制作cookies?

时间:2011-12-07 01:13:17

标签: ruby-on-rails ruby ruby-on-rails-3 sunspot

我需要帮助为我的搜索选项创建这些Cookie。我已阅读Rails GuideAPI,但仍然不明白如何正确制作Cookie。我有两个用于过滤搜索结果的复选框。它们从已经检查开始,但我需要它做的是将设置保存在cookie中,如果其中一个未被检查用于下一次搜索。这是一张更好理解的图片:

How it looks


答案:

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 个答案:

答案 0 :(得分:1)

  

Cookie是Web服务器向Web浏览器发送的消息。该   浏览器将消息存储在文本文件中。然后发送该消息   每次浏览器从中请求页面时返回服务器   服务器

在rails中,您可以使用cookies,cookies.permanent和cookies.signed来编写cookie:

  • cookies - 简单会话Cookie
  • cookies.permanent - 创建Cookie 有效期至未来20年
  • cookies.signed - 生成 已签名的cookie代表,以篡改其价值 最终用户。

为了解释cookie使用情况,我为您的应用程序创建了两个会话cookie:'online_search_checked'和'offline_search_checked'。它们包含“true”或“false”值,表示是否选中了相应的复选框。

check_box_tag具有以下参数:checkbox_name,checkbox_value,is_checked。我们需要根据从cookie接收的值修改第三个参数。第二个值可以是常数。如果选中复选框,则params [:checkbox_name] == checkbox_value。如果未选中复选框,则params [:checkbox_name] == nil。

以下是算法:

  • 当用户第一次点击“搜索/索引”时,他没有 “online_search_checked”和“offline_search_checked”Cookie设置。 然后,您希望向他显示默认选中的两个复选框。
  • 如果这不是第一次,那么用户已经设置了Cookie和 我们需要从这些cookie中恢复复选框的状态。
  • 当用户点击“搜索/结果”时,我们会使用当前状态的复选框更新Cookie 并从这些cookie中恢复复选框的状态。

以下是我的评论代码:

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

希望这有帮助!


更新

我不太明白你想在这做什么:

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?

在您的代码中params[:online_search] == 1params[:offline_search] == 0表达始终为false,因为根据{{1},params[:online_search]params[:offline_search]nil条件。