Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I save Ransack searches to the database?
    primarykey
    data
    text
    <p>I'm trying to save Ransack searches to the database. I believe I should be able to just store the <code>params[:q]</code> value, then append that to the search URL when I want to recall the search. I don't know how to save the <code>params[:q]</code> value, though.</p> <p>The URL that Ransack creates is something like this:</p> <pre><code>http://site.com/search?utf8=%E2%9C%93&amp;q%5Bone%5D=something&amp;q%5Btwo%5D=&amp;q%5Bthree%5D=&amp;q%5Blow_number%5D=0&amp;q%5Bhigh_number%5D=300000&amp;q%5Bfour%5D=&amp;commit=Search </code></pre> <p>My route to the action that will save the search is:</p> <pre><code>match 'users/:id/saved_search_add' =&gt; 'users#saved_search_add', :as =&gt; :saved_search_add </code></pre> <p>If I use this code in the view:</p> <pre><code>&lt;%= link_to('Save Search', saved_search_add_path(current_user, :q =&gt; params[:q])) %&gt; </code></pre> <p>With this code in the controller:</p> <pre><code> def saved_search_add @saved_search = Search.create(:query =&gt; params[:q].to_hash, :user_id =&gt; @user.id) respond_to do |format| if @saved_search.save format.html { redirect_to(:back) } else format.html { redirect_to(:back) } end end end </code></pre> <p>Then the data stored is:</p> <pre><code>--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess one: '' two: '' three: '' low_number: '0' high_number: '300000' four: '' </code></pre> <p>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.</p> <p>How can I get <code>params[:q]</code> into my database, to be used later to recreate the full search URL?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload