[RUBY] Ability to display a list of products

Introduction

We will summarize the product display functions that were packed in the implementation this time.

Implementation function

・ Three displays of image, product, and product name. ・ Products are displayed in order of earliest posting date. ・ Sold out is displayed for sold products.

Image / Product. 3 displays of product name

<%= @item.price %>Circle<br><%= @item.shipping_method.name %>

When listing the product this time, Active Hash was used for ** delivery method, product status, prefecture, etc. **.

Points to note

Since Active Hash is used, if the above name is the same id as the column, the id will be displayed.

Products are displayed in order of posting date

Use the order method in the index action of the controller.

oreder method

A method that changes the order of instances that have record information obtained from the table. There are two types of sort order: ASC (ascending order) and DESC (descending order).

@users = User.all.order(Column name: :Sort order)

In this case, I want to display ** from the earliest saved time **. Therefore, I used the ** (created_at) column ** to save the saved time as shown below. Since the sort order is ** new to old (descending) **, desc is used.

@items = Item.all.order(created_at: :desc)

Show sold out for sold items

The method used to implement this feature -I want to display sold out when the product is sold out using the if statement. So how can you tell when a product is sold out and not sold out? The answer can be solved by using the ** present method **.

present method

It is a method that returns true if the value of the receiver that is an object exists, and false if it does not exist. It is often used when using conditional branching such as if statements.

Variable name.present?

Used by describing as above. ?? Put together. If you use this method to display sold out on the products sold this time, it will be as follows.

<% if item.purchase.present?  %>
   <div class='sold-out'>
    <span>Sold Out</span>
   </div>
<% end %>

・ ** item is the variable name of the table containing the product information ** ・ ** purchase is the variable name of the table that contains the information of the purchased product ** These two need to form an association in advance.

Recommended Posts

Ability to display a list of products
[Rails] How to display the list of posts by category
Android Development-Try to display a dialog-
A list of rawValues for UITextContentType.
How to display products by category on the same list screen
Cast an array of Strings to a List of Integers in Java
How to output a list of strings in JSF as comma-separated strings
Why assign an ArrayList to a List
List of recommended articles related to Corda
How to sort the List of SelectItem
A story addicted to EntityNotFoundException of getOne of JpaRepository
Add empty data to the top of the list
How to display a web page in Java
Set the time of LocalDateTime to a specific time
[Ruby] Code to display the day of the week
How to display the result of form input
[Rails] I want to display the link destination of link_to in a separate tab
I want to call a method of another class
Get a list of MBean information for Java applications
[Rails, JS] How to implement asynchronous display of comments
java: How to write a generic type list [Note]
[Android] Display of input candidates using List Popup Window
Get a list of classes in a Guava specific package
How to display a browser preview in VS Code
Output a list of cron settings for all users.
[Rails] Volume that displays favorites and a list of favorites
Display the average value of the evaluation as a star
Extract a specific element from the list of objects
[IOS] To allow rotation of only a specific screen
List of beginners (List) memo
I want to get a list of the contents of a zip file and its uncompressed size
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.
How to display the select field of time_select every 30 minutes
Port C code with a lot of typecasts to Swift
[Ruby] Find a permutation consisting of 0 to 9 at high speed.
I want to make a specific model of ActiveRecord ReadOnly
I want to make a list with kotlin and java!
How to Burning a Install Disk of Windows from Ubuntu
[Rails] Function to search and list products from multi-level categories
A series of steps to create portfolio deliverables with Rails
How to move another class with a button action of another class.
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby] How to retrieve the contents of a double hash
How to implement one-line display of TextView in Android development
A program that counts the number of words in a List
I tried to make a client of RESAS-API in Java
I want to narrow down the display of docker ps
I want to use FireBase to display a timeline like Twitter
[jsoup] How to get the full amount of a document