How can I save Ransack searches to the database?

前端 未结 1 1410
长发绾君心
长发绾君心 2021-02-10 00:58

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

1条回答
  •  旧巷少年郎
    2021-02-10 01:46

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

    0 讨论(0)
提交回复
热议问题