Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将搜索首选项保存到数据库_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何将搜索首选项保存到数据库

Ruby on rails 如何将搜索首选项保存到数据库,ruby-on-rails,Ruby On Rails,我有一个高级搜索表单,我想知道如何将用户选择保存到数据库中。当前设置将执行的每个搜索保存到数据库中,这样我就可以统计用户在网站上搜索的内容 我的问题是如何将用户的选择保存到他们的帐户中,以便在他们返回网站时进行预加载,或者他们可以创建保存的搜索选项列表。我有一个社交网络应用程序,这将有助于用户,这样他们就不必在每次访问网站时输入年龄范围、邮政编码等 我不知道该怎么做 搜索控制器: def new @search = Search.new end def create

我有一个高级搜索表单,我想知道如何将用户选择保存到数据库中。当前设置将执行的每个搜索保存到数据库中,这样我就可以统计用户在网站上搜索的内容

我的问题是如何将用户的选择保存到他们的帐户中,以便在他们返回网站时进行预加载,或者他们可以创建保存的搜索选项列表。我有一个社交网络应用程序,这将有助于用户,这样他们就不必在每次访问网站时输入年龄范围、邮政编码等

我不知道该怎么做

搜索控制器:

  def new
    @search = Search.new
  end

  def create
    @search = Search.new(params[:search])
    if @search.save
      redirect_to @search
    else
      render 'new'
    end
  end

  def show
    @search = Search.find(params[:id])
    @users = @search.users
  end
搜索模型:

  def users
    @users ||= find_users
  end

    private

    def find_users
      users = User.order(:id)
      users = users.where(gender: gender) if gender.present?
      users = users.where(zip_code: zip_code) if zip_code.present?
      users = users.where(children: children) if children.present?
      users = users.where(religion: religion) if religion.present?
      users = users.where(ethnicity: ethnicity) if ethnicity.present?

      if min_age.present? && max_age.present?
        min = [ min_age, max_age ].min
        max = [ min_age, max_age ].max
        min_date = Date.today - min.years
        max_date = Date.today - max.years
        users = users.where("birthday BETWEEN ? AND ?", max_date, min_date)
        users
      end
      users
    end
  end
搜索表格:

<%= form_for @search do |f| %>
<div class="field">
  <%= f.label :gender %><br />
  <%= f.select :gender, ['male', 'female'], :include_blank => true %>
</div>

  <div class="field">
    <%= f.label :zip_code %><br />
    <%= f.text_field :zip_code %>
  </div>
  <div class="field">
    <%= f.label :children %><br />
    <%= f.select :children, [['Yes, they live with me'], ['I want kids now'], ["I want one someday"], ["Not for me"]], :include_blank => true %>
  </div>
  <div class="field">
    <%= f.label :religion %><br />
    <%= f.select :religion, [["Agnostic"], ["Atheist"], ["Christian"], ["Catholic"], ["Buddhist"], ["Hindu"], ["Jewish"], ["Muslim"], ["Spiritual without affiliation"], ["Other"], ["None"], ["Prefer not to say"]], :include_blank => true %>
  </div>
  <div class="field">
    <%= f.label :ethnicity %><br />
    <%= f.select :ethnicity, [["Asian"], ["Biracial"], ["Indian"], ["Hispanic/Latin"], ["Middle Eastern"], ["Native American"], ["Pacific Islander"], ["White"], ["Other"]], :include_blank => true %>
  </div>    <%= f.select :min_age, (18..75), :include_blank => true %>
    to
    <%= f.select :max_age, (18..75), :include_blank => true %>


  <div class="actions"><%= f.submit "Search" %></div>
<% end %>


正确%>

正确%>
正确%>
正确%> 正确%> 到 正确%>
您可以使用json、封送或任何其他序列化程序将搜索参数序列化为字符串,然后将其存储到cookie或数据库中。

您可以将搜索设置存储在。缺点是,当客户端从不同的计算机或浏览器访问站点时,没有保存的搜索设置,因为会话id存储在cookie中

如果您确实希望在数据库中保留设置,可以在
用户
模型上创建一个
搜索设置
字段,并将设置序列化到此字段。如果您使用的是Postgresql,则可以使用
数组
hstore
列类型