DEV Community

Cover image for Creating Ransack params without search_form_for
Robert Hustead
Robert Hustead

Posted on

Creating Ransack params without search_form_for

Sometimes you have a search box in one area, but you want to display the results of that search with a totally separate controller and view. It can be a little confusing trying to do so with a gem like Ransack, so this tutorial aims to give a basic run down of how you could implement search in one controllers action, and display the results in another controllers action.

First, you'll need to ensure the Ransack gem is in your Gemfile and you run bundle install. You can find further documentation for Ransack here: GitHub - activerecord-hackery/ransack: Object-based searching. This tutorial assumes basic knowledge of how to use Ransack.

Normally, when using Ransack, you'll have a form that uses the search_form_for helper that Ransack provides. Something like:

# in index.html.erb

<%= search_form_for @q do |f| %>

  # Search if the name field contains...
  <%= f.label :name_cont %>
  <%= f.search_field :name_cont %>

  <%= f.submit %>
<% end %>
Enter fullscreen mode Exit fullscreen mode

and in your controller you'll have something like:

# in ItemsController.rb

def index
  @q = Person.ransack(params[:q])
  @people = @q.result(distinct: true)
end
Enter fullscreen mode Exit fullscreen mode

Pretty standard stuff. But what if you want to render a different view with the Ransack results? For example, you want to send users to a specific 'Search Results' page using your Search Controller?

To do this, you'll need to get the search query from the user and format it in a way that Ransack wants to receive it using basic form helpers. Specifically, Ransack wants a parameter with the key 'q', which contains the value of another hash. The hash value of 'q' is a single key value pair where the key is a string containing the methods for how Ransack should look for the object, and the value of that key is a string that is the actual search query from the user.

Basically, your params variable should look something like this:

params = {"q": {"name_or_description_or_id_cont": "Actually query from the User"}}
Enter fullscreen mode Exit fullscreen mode

You can create this 'q' param that Ransack expects explicity with the following:

# in whatever view you'd like users to search from

<%= form_tag search_path, method: :get do %>
  <%= text_field_tag :"q[name_or_description_or_id_cont]" %>
  <%= submit_tag "Search", data: { disable_with: "Searching..." }
<% end %>
Enter fullscreen mode Exit fullscreen mode

This form will create our 'q' param and format it in the way Ransack wants it, and send it along to whatever Controller and Action you set up in your routes. You can of course change the name_or_description_or_cont to whatever fits your needs.

In the Controller you want to use to display the results, you will have the normal Ransack process

  # in whatever controller you'd like to handle the search request

  def search
    @q = Item.ransack(params[:q])
    @items = @q.result
  end
Enter fullscreen mode Exit fullscreen mode

You can now use the @items instance variable in your view to display the results of the query.

Good luck, and happy coding!

Latest comments (0)