This time we will implement a search form.
This time, we will implement something that allows you to search for articles with specified keywords by searching for the title of the article. : running :: dash:
routes
This time I will use "collection" Use "member" if you want to identify the data. If you use "member", the routing will have an "id".
It is written like this. ↓
Also, if you want to receive ": id" in params when moving the action, use member and If you don't need to go to a specific page by specifying an id, use collection to add an action.
I investigated Rails related things: Learn more about collections and members in routes.rb
And here is another reference.
config/routes.rb
resources :albums do
collection do
get 'search'
end
end
controller
Use the "search" method for the controller and write "(params [: keyword])"
Allows you to get ": keyword".
controllers/albums_controller.rb
def search
@albums = Album.search(params[:keyword])
end
models
The search content is specified here.
The Where method and LIKE have been learned before. Click here for details
Use "LIKE" You can also use special symbols with "LIKE".
"%" Percent sign → Arbitrary character string (including blank character string) (1 or more characters)
"_" Underscore symbol → Any one character
Click here for easier understanding
pikawaka: [Rails] Use the where method to get the data you want!
models/album.rb
def self.search(search)
if search !="" #If it is not empty, it will be searched.
Album.where('title LIKE(?)', "%#{search}%")
else
Album.all
end
end
views
The part to be searched will be implemented using "form_with".
Write ": keyword" in the "text_field" part.
If you set the routing as the beginning, it looks like this.
As described in the controller part, when sending
": Keyword" (word you want to search) goes into params
Search with the where method
It will be sent to "search_albums_path" in the url part.
html:views/pics/index.html.erb
<%= form_with(url: search_albums_path, local: true, method: :get, class: "search-form") do |f| %>
<div class="search-inside">
<%= f.text_field :keyword, placeholder: "Search for articles", class: "search-box" %>
</div>
<div class="search-inside">
<%= f.submit "Search", class: "search-btn btn" %>
</div>
<% end %>
The search part looks like this. The CSS part is omitted, but you can decorate it as you like.
The part to be displayed is described like this.
html:views/albums/search.html.erb
<% @albums.each do |album| %>
<li class="list">
<%= link_to album_path(album) do %>
<div class="pic-img-content">
<%= image_tag album.image, class: "pic-img" if album.image.attached? %>
</div>
<div class="pic-info">
<div class="pic-title">
<p><%= album.created_at %></p>
<h3><%= album.title %></h3>
</div>
<div class="pic-text">
<div class="text-box">
<p><%= album.text %></p>
<div>
</div>
</div>
<% end %>
</li>
<% end %>
It took a while, but I was able to implement it safely.
Next time, I want to be able to do something a little different sooner. : sunny:
Recommended Posts