ruby

Is Ransack the Secret Ingredient to Supercharge Your Rails App Search?

Turbocharge Your Rails App with Ransack's Sleek Search and Sort Magic

Is Ransack the Secret Ingredient to Supercharge Your Rails App Search?

If you’re working on a Ruby on Rails application and want to add some stellar search functionality, Ransack is your answer. This gem is often the superhero in the Rails world when it comes to search forms, customizable filters, and all sorts of sorting options. Let’s dive into how you can get Ransack up and running in your Rails app.

Getting Started

First, let’s talk about getting Ransack into your Rails playground. You need to tweak your Gemfile a bit to include Ransack. Just pop in this line:

# Gemfile
gem 'ransack'

And then run the good old bundle command:

bundle install

Boom! You’ve got Ransack ready to rock.

Creating a Scaffold

Imagine you’re building an application for managing articles. Setting up a scaffold is the way to go. This will magically create your model, controller, and views.

rails g scaffold Article title body:text
rails db:migrate

Seeding the Database With Faker

Now, your app needs some data to play around with. Faker gem to the rescue! You can seed your database with some funky sample data.

# db/seeds.rb
50.times do |x|
  Article.create(title: Faker::Lorem.sentence, body: Faker::Lorem.paragraph(sentence_count: 5))
end

Then, run the seeder:

rails db:seed

Controller Setup

Time to get down to business and tweak your controller to handle search queries with Ransack. Head over to your index method and make some changes.

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def index
    @q = Article.ransack(params[:q])
    @articles = @q.result(distinct: true)
  end
end

Building the Search Form

With the foundation laid, let’s create a search form in your view. Ransack has got a nifty search_form_for helper that makes this a walk in the park.

<!-- app/views/articles/index.html.erb -->
<%= search_form_for @q do |f| %>
  <%= f.search_field :title_or_body_cont, placeholder: "Search..." %>
  <%= f.submit "Search!" %>
<% end %>

<h1>Articles</h1>
<table>
  <thead>
    <tr>
      <th><%= sort_link(@q, :title, "Title", default_order: :asc) %></th>
      <th><%= sort_link(@q, :body, "Body", default_order: :asc) %></th>
    </tr>
  </thead>
  <tbody>
    <% @articles.each do |article| %>
      <tr>
        <td><%= article.title %></td>
        <td><%= article.body %></td>
      </tr>
    <% end %>
  </tbody>
</table>

Advanced Search Options

Looking to take things up a notch with advanced search options? Ransack’s got your back. You can search for exact matches, partial matches, and even filter across multiple fields.

<!-- app/views/articles/index.html.erb -->
<%= search_form_for @q do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title_cont, class: 'form-control' %>

  <%= f.label :body %>
  <%= f.text_field :body_cont, class: 'form-control' %>

  <%= f.label :year_eq %>
  <%= f.number_field :year_eq, class: 'form-control' %>

  <%= f.label :price_gteq %>
  <%= f.number_field :price_gteq, class: 'form-control' %>

  <%= f.label :price_lteq %>
  <%= f.number_field :price_lteq, class: 'form-control' %>

  <%= f.submit class: 'btn btn-primary' %>
<% end %>

<%= link_to "Clear", request.path, class: "btn btn-default" %>

Sorting

Sorting your data just got easier with Ransack. The sort_link helper does the heavy lifting here.

<!-- app/views/articles/index.html.erb -->
<table>
  <thead>
    <tr>
      <th><%= sort_link(@q, :title, "Title", default_order: :asc) %></th>
      <th><%= sort_link(@q, :body, "Body", default_order: :asc) %></th>
    </tr>
  </thead>
  <tbody>
    <% @articles.each do |article| %>
      <tr>
        <td><%= article.title %></td>
        <td><%= article.body %></td>
      </tr>
    <% end %>
  </tbody>
</table>

Combining Search Fields

Why settle for one search field when you can combine multiple? Ransack’s or matcher lets you hit two birds with one stone.

<!-- app/views/articles/index.html.erb -->
<%= search_form_for @q do |f| %>
  <%= f.search_field :title_or_body_cont, placeholder: "Search..." %>
  <%= f.submit "Search!" %>
<% end %>

Filtering on Associations

Got an Article model linked to an Author model? No worries. Ransack lets you filter on associations too.

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def index
    @q = Article.ransack(params[:q])
    @articles = @q.result(distinct: true)
  end
end
<!-- app/views/articles/index.html.erb -->
<%= search_form_for @q do |f| %>
  <%= f.search_field :author_name_cont, placeholder: "Search by Author..." %>
  <%= f.submit "Search!" %>
<% end %>

Advanced Features

Ransack isn’t just a one-trick pony. It comes packed with internationalization (i18n) support and loads of other configurations. You can tweak the search form and results to fit your application’s vibes.

Real-World Example

Let’s paint a picture. You’re building a real estate application where users can search for houses based on criteria like the number of bedrooms, the presence of a lift, and price range. Here’s a little teaser of how you’d implement this using Ransack:

<!-- app/views/houses/index.html.erb -->
<%= search_form_for @q, url: search_results_path do |f| %>
  <%= f.select :bedrooms_eq, House.pluck(:bedrooms, :id), {prompt: 'Select number of bedrooms'} %>
  <%= f.select :with_lift_eq, ['Yes', 'No'], {prompt: 'Lift availability'} %>
  <%= f.number_field :price_gteq, placeholder: 'Minimum price' %>
  <%= f.number_field :price_lteq, placeholder: 'Maximum price' %>
  <%= f.submit "Search!" %>
<% end %>

<%= link_to "Clear", request.path, class: "btn btn-default" %>

And your controller to handle it:

# app/controllers/houses_controller.rb
class HousesController < ApplicationController
  def index
    @q = House.ransack(params[:q])
    @houses = @q.result(distinct: true)
  end

  def results
    @q = House.ransack(params[:q])
    @results = @q.result
  end
end

Just imagine how clean and helpful such a search feature would be for potential home buyers.

Conclusion

Ransack brings a whole new level of ease and functionality when it comes to adding a search feature to your Rails application. It’s simple to implement, highly customizable, and makes your app’s user experience smoother and more efficient. Whether you’re building a blog, an enterprise app, or anything in between, Ransack is a game-changer. So go on, give it a whirl and watch your app reach new heights of awesomeness.

Keywords: Ruby on Rails, Ransack gem, search functionality, search forms, customizable filters, Rails scaffold, Faker gem, database seeding, search form implementation, advanced search options.



Similar Posts
Blog Image
Are You Ready to Unlock the Secrets of Ruby's Open Classes?

Harnessing Ruby's Open Classes: A Double-Edged Sword of Flexibility and Risk

Blog Image
10 Essential Security Best Practices for Ruby on Rails Developers

Discover 10 essential Ruby on Rails security best practices. Learn how to protect your web apps from common vulnerabilities and implement robust security measures. Enhance your Rails development skills now.

Blog Image
Is Your Rails App Missing the Superhero It Deserves?

Shield Your Rails App: Brakeman’s Simple Yet Mighty Security Scan

Blog Image
**8 Proven Strategies to Boost GraphQL Performance in Rails Applications**

Boost GraphQL performance in Rails with 8 proven techniques: query complexity analysis, batch loading, persistent queries & caching strategies for faster APIs.

Blog Image
Complete Guide to Distributed Tracing Implementation in Ruby Microservices Architecture

Learn to implement distributed tracing in Ruby microservices with OpenTelemetry. Master span creation, context propagation, and error tracking for better system observability.

Blog Image
Is Your Rails App Ready for Effortless Configuration Magic?

Streamline Your Ruby on Rails Configuration with the `rails-settings` Gem for Ultimate Flexibility and Ease