Implement Ransack Gem in Ruby on Rails
“Searching” is the most common feature of most of the web applications irrespective of size, small or enterprise grade. But how do you implement this feature in web applications?
Searching with Ransack in Ruby on Rails Application
Ransack gem is a very powerful and feature-rich gem used widely by Rails community to implement advanced search capability in a Ruby on Rails application. You can create simple as well as advanced search forms for with this Rails search gem.
Let’s build a small search app using simple and advanced search options for implementing search in Rails.
First of all, add Ransack gem to your gemfile and bundle.
gem 'ransack', github: 'activerecord-hackery/ransack'
Ruby on Rails developers can first create a scaffold for Student details which we can use further to explore different search options provided by Ransack.
$ rails g scaffold Student name age:integer marks:integer
After generating Student scaffold you have below code in your student index view
As we are going for search directly we don’t need a new student link in view so remove below link from index view,
In app/views/students/index.html.erb [Remove below line from index.html]
<%= link_to 'New Student', new_student_path %>
To add few student records in database add below lines of code in your seed file
To create a database with desire student schema with few records write below command in terminal.
$ rails db:setup
Above command will create a database with scaffold migration and student records.
Ransack provides form helper
search_form_for which you can think of as an extension to
form_for with some metaprogramming magic to give you a powerful DSL specifying predicates. When the form generated by
search_form_for is submitted, the handler(controller) creates Ransack::Search and validates it.
Here we are searching by name in the Student table. So add below code for the search to students’ index view page above the table or elsewhere you want to display searching form in the view.
Here we have name field in our table, then we have to mention it like “
name_cont” in helper
search_form_for. Here we used “
_cont” matcher which means Contains value in “
name” field. If we did not follow the conventions then ransack will return the full list of data without proper searching.
Here is how our controller will look like.
@q is a ransack::search object using a parameter which is in
#result method of ransack returns an ActiveRecordRelation object with matching result which we can utilize to display in view files.
The above code gives the result of search like below:
Ransack provides different search matches as below. We can apply any of them based on our requirement.
Here is the list of all available matches which you can as per the needs.
We can easily integrate sorting functionality as well using
#sort_link. See below code for that:
After adding above sort link it will look like below.
Now, let’s implement a bit advanced search tool.
To achieve this, we have to add a few more lines of code in search_form_for file like given below:
For an advanced search, ransack uses nested structures of input fields. It groups all conditions and each of that group has an attribute name and value which is a search value on a specific attribute.
So the above code results in the following view:
The first dropdown contains table attributes. This is the default behavior. The middle one is for predicates for ransack matchers and text field for values to be searched.
Here we can also restrict to few attributes on which we want to give permission for searching. Just configure table fields name on its model file as below to customize it.
Here we are allowing to search on name and marks fields only.
That’s it! You can build your complex search forms with complex queries using Ransack easily.
Hire BoTree for comprehensive Ruby on Rails development services to implement features in your Rails web app.
Consulting is free – let us help you grow!
Choose Your Language
- Digital Marketing
- IT Consulting
- Project Management