How to create a super basic search form in Ruby on Rails

I'm a passionate software developer.
Search for a command to run...

I'm a passionate software developer.
No comments yet. Be the first to comment.
Components Introduction In order to truly understand props in React, we first need to be clear on what components are. A component is a reusable piece of the user interface that encapsulates structure, styling, and behavior. Instead of repeating the ...

Introduction Context By default, Codespace is using the GITHUB_TOKEN env var (the “Codespaces token”), which can’t create repos. It’s necessary to authenticate gh with a token/account that has the right scopes, or create the repo on the web first. Ob...

Solution Here it is: git commit --allow-empty-message -m "" Explanation To create a Git commit without a message, or with an empty message, you can use the --allow-empty-message flag with the git commit command. git commit --allow-empty-message -m "...

Steps Cleanup phase This phase’s goal is to removes existing containers, volumes, networks and images Stop and remove volumes docker compose down --volumes --remove-orphans The previous command stops and removes containers, networks, and default volu...

Steps Stop the service It’s not mandatory, but before uninstalling, it's a good practice to stop any running Docker services. sudo systemctl stop docker Remove docker packages It’s used to completely and forcefully remove a list of specified packag...

TL; DR: Seriously, this form implementation is incredibly simple
How to create a super basic search form in Ruby on Rails.
The following picture is a snapshot of an example.

Let's consider a Product model with a title attribute.
<%= form_tag('/products', method: 'get', local: true) do %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>
form_tag is a helper method provided by Rails for creating HTML forms.
'/products' is the URL or path to which the form will be submitted. The '/products url is due to the index route.
local: true is an optional parameter that specifies whether the form should be processed locally (within the same controller) or sent to a different controller for processing. In this case, local: true indicates that the form will be processed by the same controller.
Add a search method to the desired model.
# app/models/product.rb
def self.search(search)
if search
where(["title LIKE ?","%#{search}%"])
else
all
end
end
The purpose of this code is to perform a search operation on the model's associated table based on the provided search query.
If search does not have a value (i.e., it is nil or false), it executes the code block inside the else statement. The keyword all is an ActiveRecord query method that retrieves all records from the associated table.
Add the search method inside the controller.
# app/controllers/products_controller.rb
def index
@products = Product.search(params[:search])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
Product.search(params[:search]) calls the search class method on the Product model, passing params[:search] as an argument. It performs a search operation based on the provided search query.
params[:search] retrieves the value of the search parameter from the request parameters. It represents the search query entered by the user.

This solution does not adhere to solid design principles and may not be suitable for large-scale projects, as it lacks a focus on reusability.
When working with Rails forms, it is recommended to follow best practices for enterprise projects, such as utilizing form objects, query objects, and adopting filterable approaches using solid principles.
In conclusion, this article has demonstrated how to create a super basic search form in Ruby on Rails.
By following the step-by-step guide, you have learned the essential components and techniques needed to implement a basic search functionality in your Rails application.
I hope this article helped you. Let me know if you have any questions.
Your thoughts, suggestions and corrections are more than welcome.
By the way, feel free to drop your suggestions on new blog articles.
Hope to see you next time.