How to display products by category on the same list screen

Steps required to display products belonging to each category using the same index file

When creating an EC site, etc., ** I want to display products for each category on one index page !! ** I think that you may encounter a situation. For example, on a fashion e-commerce site, if you press the men's category, the products that belong to men's will be displayed, and if you select the outerwear, only the outerwear will be displayed on the list screen. It is a function that is installed in any site as a matter of course, but if you try to implement it from the developer's point of view, many people may be wondering what kind of processing should be done.

In that case, such a condition can be cleared by following the procedure below. Let's get excited now.

(1) Create a partial template that describes the notation of the category link used on the product list screen.

erb:app/views/products/category_search.html.erb


:
<tbody>
    <% categories.each do |category| %>
    <tr>
      <% if category.is_active == true %> 
      <td>
        <%= link_to category.name, products_path(category_id: category.id), class: "text-secondary" %>
      </td>                    <%# products_category as an argument to path_pass id%>
      <% end %>
    </tr>
    <% end %>
</tbody>
:

First, prepare a link corresponding to each category in each statement. Specify the index page where this description is written as the link destination, but at that time it is necessary to give the category id to the argument of path. By the way, don't forget to describe the model because an association is required to perform the above processing.

** The above description corresponds to this part on the screen ** By the way, it's my hobby that the product has become Marilyn Monroe, so I'd like you to convert it to shortcake in your brain.      ↓ スクリーンショット 2020-12-22 17.25.31.png

(2) Reflect only the products that belong to the category on the index page based on the arguments received by the controller.

Started GET "/products?category_id=1" for 118.240.8.212 at 2020-12-25 12:54:22 +0000
Cannot render console from 118.240.8.212! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ProductsController#index as HTML
  Parameters: {"category_id"=>"1"}
  Category Load (0.2ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/products_controller.rb:8
  Rendering products/index.html.erb within layouts/application

Next, the process of finding the target data based on the parameter value is performed in the controller. As a result of ①, it can be seen that ** category_id ** is assigned to the parameter and sent as described above. Write a program to first identify the category based on that id, and then identify the product associated with the category.

app/controllers/products_controller.rb


class ProductsController < ApplicationController

  def index
    @products = Product.all
    @categories = Category.all
    @tax = TAX
    if params[:category_id].present?
      #params with present method[:category_id]Check if the value is included in=>If true, execute the following
      @category = Category.find(params[:category_id])
      @products = @category.products
    end
  end

This makes it possible to display only the products associated with ** category_id = x ** on the index page. By the way, if you check the URL of the view page displayed at this time, it will be written as (... com/products? Category_id = 1), but this reflects the argument (category_id) passed to the path of the link. What was done `. Anyway, this completes the procedure for displaying products in each category. Please give it a try.

If you find any mistakes in the information, please contact us. We are looking forward to hearing from you.

Recommended Posts

How to display products by category on the same list screen
[Rails] How to display the list of posts by category
How to return to the previous screen by Swipe operation
How to move to the details screen by clicking the image
Display the list in setDetails on the screen with spring-security
How to run React and Rails on the same server
How to call multiple names at once in the same category
[Rails 5] How to display the password change screen when using devise
How to change the timezone on Ubuntu
How the website is displayed on the screen
How to sort the List of SelectItem
How to pass the value to another screen
Ability to display a list of products
Pre-processing to display on the browser (compiler)
[Rails] How to decide the destination by "rails routes"
[Ruby on Rails] How to display error messages
[Android] List all setting items on the setting screen
How to display the result of form input
[Java] Memo on how to write the source
How to display the amount of disk used by Docker container for each container
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
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
List how to learn from Docker to AKS on AWS
[Ruby on Rails] How to change the column name
[Android Studio] [Java] How to fix the screen vertically
How to change the process depending on the list pressed when there are multiple ListViews
How to start TOMCAT by specifying JAVA_HOME on Windows
I want to place RadioButtons in the same RadioGroup at any position on the screen.
Install MySQL 5.6 on CentOS6 [How to specify the version]
[Rails] How to display an image in the view
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
How to transition from the [Swift5] app to the iPhone settings screen
How to display the select field of time_select every 30 minutes
How to dynamically change the column name acquired by MyBatis
I want to use screen sharing on the login screen on Ubuntu 18
How to disable Set-Cookie from API on the front side
How to display a graph in Ruby on Rails (LazyHighChart)
Display an error screen during the download process to response
How to add the same Indexes in a nested array
How to install JDK 8 on Windows without using the installer
How to switch Java in the OpenJDK era on Mac
I want to transition to the same screen in the saved state
[Rails] How to display information stored in the database in view
How to check the database of apps deployed on Heroku
How to deploy on heroku
[Java] How to display Wingdings
[Android] Try to display the effect on the touched part of the screen, which is common in games.
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Swift] How to display when the quiz app answers correctly [Beginner]
[RSpec on Rails] How to write test code for beginners by beginners
How to automatically operate a screen created in Java on Windows
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to install and configure the monitoring tool "Graphite" on Ubuntu
[Rails] How to omit the display of the character string of the link_to method
[Behavior confirmed in December 2020] How to implement the alert display function
[Rails] How to put a crown mark on the ranking function
How to place and share SwiftLint config files on the server
I tried to display the calendar on the Eclipse console using Java.