How can I save Ransack searches to the database?

拥有回忆 提交于 2019-12-03 09:06:33

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

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

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 %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!