When multiple images were posted at the same time, I had to devise a description to display the images. I will post the article as a memo so that I will not forget the contents.
** I want to display the one with the lowest index number of multiple posted photos on the index screen **
File name.html.haml
section.fifth-headline
%h2.title Pickup category
.contentsbox
.contents-title
%h3.heading Newly posted products
.contents-lists
.contents-list
- @newProducts.each do |product|
=link_to "#" do
.contents-list__itmes
.contents-list--item
= link_to product_path(product), class: "listBtn" do
%figure.product__images
= image_tag product.images[0].name.url, alt: "image_url",size: "200x150", class:"new-img"
.contents-list--inner
%h3.name
= product.name
.data
%ul
%li
= product.price
Circle
%li
%i.fa.fa-star.likeIcon 0
%p (tax included)
home_controller
def index
@newProducts = Product.includes(:images).where(status: 0).order("RAND()")
~abridgement~
end
If you have posted multiple images and want to display only the first one, you can get the index number [0] by writing as follows.
= image_tag product.images[0].name.url, alt: "image_url",size: "200x150", class:"new-img"
Also, in this case, you have to define the file to be called by the index action. Therefore, **. Where (status: 0) ** is defined and assigned to the instance variable.
@newProducts = Product.includes(:images).where(status: 0).order("RAND()")
I think this will work!
This time, "I can write like this! I thought, and wrote an article! I'm still studying, so if you find something wrong, please comment!
Thank you very much!
Recommended Posts