[RUBY] Implement search function with form_with


Use Rails form_with to send the information you want to search to the controller and I will write how to implement the function to be listed on the index page.

What you want to achieve

This time, I will post a post that includes the word I searched for from the Post table in the body I would like to list it in index.html.erb of the Post controller.

[Execution environment] Ruby 2.7.2 Rails

Send search conditions


<%= form_with url: posts_path, method: :get, local: true do |f| %>
  <%= f.label :post_key, 'Search' %>
  <%= f.text_field :post_key %>
  <%= f.submit, 'Search for' %>
<% end %>

This time, the search result list will be displayed on the index page, so enter the path corresponding to the index for url. If the page you want to display is different from the index, enter the url corresponding to the page you want to display.

By specifying method as get, it goes through the routing that leads to index and becomes an index action. You can send the value you want to search. If you do not specify this, the method will be sent by post and an error will occur.

Since the value you want to search is stored in: post_key, You can specify any word other than: post_key as long as it is common to the word written in the controller.

Search result list display controller


def index
  if params[:posts_key]
    @posts = Post.where(params[:posts_key])
    @posts = Post.all

The: posts_key sent from the input form will get you here. The behavior of else is that if you press the search button without entering anything, all posts will be displayed.

List search results on index page


<p>"search results: <%= @posts.count %>Case</p>
<ul class="posts">
  <%= @posts.each do |post| %>
    <li class="post">
      <%= post.content %>
  <% end %>

This time I implemented it without using page nation, so The search result list is displayed by executing the iterative process using each method.

The count method is used to display the number of search results.

At the end

I think that the search function can be implemented by the above procedure! Feel free to comment if there are any deficiencies or uncertainties I would appreciate it if you could! Thank you for reading until the end!

Recommended Posts

Implement search function with form_with
[Rails] Implement search function
Implement search function with Rakuten Books (DVD) API
[Rails] Implement User search function
Try to implement login function with Spring-Boot
How to implement TextInputLayout with validation function
Implement paging function with Spring Boot + Thymeleaf
Create an or search function with Ransack.
Implement partial match search function without using Ransuck
Let's make a search function with Rails (ransack)
Implementation of search function
Rails search function implementation
Search function [copype implementation]
With Kotorin ―― 7. Scoping Function
Serverless Function with Micronaut
[Implementation procedure] Implement image upload function with Active Storage
Rails learning How to implement search function using ActiveModel
Implement application function in Rails
[For those who create portfolios] Search function created with ransack
Rails fuzzy search function implementation
Introduced graph function with rails
Search function using [rails] ransack
Implement GraphQL with Spring Boot
Java to play with Function
Implementation of sequential search function
Implement follow function in Rails
Use ransack Search function implementation
[Rails 6] Implementation of search function
Implement jCaptcha reload with ajax
Login function with Spring Security
Login function implementation with rails
[Rails] Implement image posting function
Implement login function simply with name and password in Rails (3)
Implement a refined search function for multiple models without Rails5 gem.
Let's roughly implement the image preview function with Rails + refile + jQuery.
I tried to implement the image preview function with Rails / jQuery
Implemented authentication function with Spring Security ②
Implement UICollectionView of iOS14 with CustomCell.
Real-time comment function with Action Cable (2/2)
Item 86: Implement Serializable with great caution
Implement text link with Springboot + Thymeleaf
[Java EE] Implement Client with WebSocket
Implemented authentication function with Spring Security ③
Implemented mail sending function with rails
Try to implement iOS14 Widget function
Fibonacci sequence with (memoization) recursive function
Create pagination function with Rails Kaminari
Implement simple login function in Rails
Implement tagging function in form object
Implemented authentication function with Spring Security ①
[Swift 5] Implement UI for review function
Implement search functionality using LIKE clauses
Implement CSV download function in Rails
Timeless search with Rails + JavaScript (jQuery)
[Rails] Implement the product purchase function with a credit card registered with PAY.JP