bo-oz
bo-oz

Reputation: 2872

How to add a search parameter to an existing Ransack query

I have a search form on a page, that allows a user to filter a list on multiple conditions. Now I'd like to add quick links on top of the list to apply scopes. I tried it like this:

= link_to "Visited", q: {"activated" => true}

While this filters the list to show only activated items, it also resets the search query. In other words, it doesn't remember what was already filtered in the form.

Is there a way to adapt @q so that I can add this "activated" => true to the hash of required filters?

Upvotes: 1

Views: 2518

Answers (2)

Dipil
Dipil

Reputation: 2708

Assuming you're only using the :q param to filter, you could aggregate that.

= link_to "Visited", q: (params[:q] || {}).merge(activated: true)

Upvotes: 1

Mark Davies
Mark Davies

Reputation: 796

I don't think you can because if you follow a link you are not submitting the form therefore the parameters are not going to be submitted.

Passing the params in your link to will send the params if any exist:

= link_to "Visited", q: {"activated" => true, "your_params" => params}

This will only work if the form has been submitted once though, otherwise the params would be empty.

EDIT

I assume that the fields on your forms are populating if there is a value.

For example,

<%= text_field_tag(:email, if !params["email"].nil? then params["ip_email"] end) %>

Upvotes: 0

Related Questions