如何将 Ransack 搜索保存到数据库? [英] How can I save Ransack searches to the database?

查看:50
本文介绍了如何将 Ransack 搜索保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Ransack 搜索保存到数据库中.我相信我应该能够只存储 params[:q] 值,然后在我想回忆搜索时将其附加到搜索 URL.不过,我不知道如何保存 params[:q] 值.

I'm trying to save Ransack searches to the database. I believe I should be able to just store the params[:q] value, then append that to the search URL when I want to recall the search. I don't know how to save the params[:q] value, though.

Ransack 创建的 URL 是这样的:

The URL that Ransack creates is something like this:

http://site.com/search?utf8=%E2%9C%93&q%5Bone%5D=something&q%5Btwo%5D=&q%5Bthree%5D=&q%5Blow_number%5D=0&q%5Bhigh_number%5D=300000&q%5Bfour%5D=&commit=Search

我执行保存搜索的操作的路线是:

My route to the action that will save the search is:

match 'users/:id/saved_search_add' => 'users#saved_search_add', :as => :saved_search_add

如果我在视图中使用此代码:

If I use this code in the view:

<%= link_to('Save Search', saved_search_add_path(current_user, :q => params[:q])) %>

在控制器中使用此代码:

With this code in the controller:

  def saved_search_add
    @saved_search = Search.create(:query => params[:q].to_hash, :user_id => @user.id)

    respond_to do |format|
      if @saved_search.save
        format.html { redirect_to(:back) }
      else
        format.html { redirect_to(:back) }
      end
    end
  end

那么存储的数据为:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
one: ''
two: ''
three: ''
low_number: '0'
high_number: '300000'
four: ''

所以,我需要在数据进入数据库时​​以某种方式对其进行格式化(也许还有数据出来时?)...我已经阅读了一些关于序列化它的内容,但我没有太多运气得到它工作.

So, I need to somehow format the data as it's going into the database (and perhaps also as it's coming out?)... I've read something about serializing it, but I haven't had much luck getting that to work.

如何将 params[:q] 放入我的数据库中,以便稍后用于重新创建完整的搜索 URL?

How can I get params[:q] into my database, to be used later to recreate the full search URL?

推荐答案

我最终只使用了 request.fullpath 而不是 params[:q].

I ended up just using request.fullpath instead of params[:q].

将查询保存到数据库的代码在Users控制器中:

The code that saves the query to the database is in the Users Controller:

def saved_search_add
  @saved_search = Search.create(:query => params[:q], :user_id => current_user.id)

  respond_to do |format|
    if @saved_search.save
      format.html { redirect_to(:back) }
    else
      format.html { redirect_to(:back) }
    end
  end
end

我在视图中用于将搜索查询发送到 Users 控制器的代码是:

The code I use in my View to send the search query to the Users Controller is:

<%= link_to('Save Search', saved_search_add_path(current_user, :q => request.fullpath)) %>

查询值在数据库中存储为:

The query value is stored in the database as:

/search?utf8=%E2%9C%93&q%5Bone%5D=something&q%5Btwo%5D=&q%5Bthree%5D=&q%5Blow_number%5D=0&q%5Bhigh_number%5D=300000&q%5Bfour%5D=&commit=Search

我在视图中创建了指向该已保存搜索的链接:

I create the link to that saved search in the View with:

<%= link_to "Saved Search", search.query %>

这篇关于如何将 Ransack 搜索保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆