[Rails] How to display the list of posts by category

Premise

manner

Controller Before editing

gears_controller.rb


 def index
   @user = current_user
   @gear = Gear.where(user_id: @user.id)
 end

After editing

gears_controller.rb


  def index
    @user = current_user
    @gear1 = Gear.where(user_id: @user.id, category: "Residential system")
    @gear2 = Gear.where(user_id: @user.id, category: "Cooking system")
    @gear3 = Gear.where(user_id: @user.id, category: "Fire system")
    @gear4 = Gear.where(user_id: @user.id, category: "Other")
  end

View

html:index.html.erb


<div class="category bg-success">Residential system</div>
  <% @gear1.each do |gear| %>
    <div class="gear-index-item mb-20">
      <% if gear.image.attached? %>
        <%= image_tag gear.image, class: "index-img" %>
      <% else %>
        <img class="index-img" src="<%= "/images/default_gear.jpg " %>" alt="Index image cap">
      <% end %>
      <%= link_to(gear.name, "/gears/#{gear.id}") %>
    </div>
  <% end %>
  
  <div class="category bg-warning">Cooking system</div>
  <% @gear2.each do |gear| %>
    <div class="gear-index-item mb-20">
      <% if gear.image.attached? %>
        <%= image_tag gear.image, class: "index-img" %>
      <% else %>
        <img class="index-img" src="<%= "/images/default_gear.jpg " %>" alt="Index image cap">
      <% end %>
      <%= link_to(gear.name, "/gears/#{gear.id}") %>
    </div>
  <% end %>
   .
   .
   .

Iterate with each statement.

result

image.png

Summary

If you use the where method, you can easily organize them in a specific column!

Recommended Posts

[Rails] How to display the list of posts by category
How to display products by category on the same list screen
How to sort the List of SelectItem
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to decide the destination by "rails routes"
How to display the result of form input
How to make the schema of the URL generated by Rails URL helper https
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
How to remove the underline displayed by Rails link_to
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to change the column name of the table
[Rails] How to get the contents of strong parameters
[Rails] How to display an image in the view
How to display the amount of disk used by Docker container for each container
[Rails] Implementation procedure of the function to tag posts without gem + the function to narrow down and display posts by tags
How to display the select field of time_select every 30 minutes
[rails] How to display db information
How to connect the strings in the List separated by commas
Let's summarize how to extend the expiration date of Rails
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Rails] How to display information stored in the database in view
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
Summary of stumbling blocks related to form_with
How to sort the List of SelectItem
Ability to display a list of products
How to delete / update the list field of OneToMany
List of beginners (List) memo
[Rails] How to display the list of posts by category
[Rails] How to get the URL of the transition source and redirect
How to display 0 on the left side of the standard input value
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to change the page title of the browser for each page
[Rails 5] How to display the password change screen when using devise
[Rails] How to use the map method
[Rails] How to display error messages individually
The process of introducing Vuetify to Rails
Ability to display a list of products
[Rails] How to use video_tag to display videos
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
Display character strings character by character [Note]
[Rails] How to omit the display of the character string of the link_to method
How to decorate the radio button of rails6 form_with (helper) with CSS
How to solve the local environment construction of Ruby on Rails (MAC)!
How to display the text entered in text_area in Rails with line breaks
[Ruby On Rails] How to search the contents of params using include?
[Rails] How to solve the time lag of created_at after save method
How to find the cause of the Ruby error
[Rails] Button to return to the top of the page
[Rails] Introduction of pry-rails ~ How to debug binding.pry
[Ruby on Rails] How to display error messages
Customize how to divide the contents of Recyclerview
How to output CSV created by Rails to S3
How to get today's day of the week
[Ruby] Code to display the day of the week
How to separate .scss by controller in Rails
How to use JQuery in js.erb of Rails6
[Java] How to get the authority of the folder
How to check Rails commands in the terminal
[Rails] How to convert UC time display to Japanese time display
[Ruby on Rails] How to make the link destination part of the specified id
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to make a unique combination of data in the rails intermediate table
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to embed and display youtube videos in Rails (You can also get the URL you entered by editing)
How to write Rails
How to uninstall Rails