When implementing the function of displaying the products listed as an issue in a list I was writing a conditional expression in an if statement
that is,
something like
if statement This started to work enthusiastically with ease I finished writing and confirmed the behavior ...
items_controller.rb
def index
@items = Item.order('created_at DESC')
end
ruby:index.html.erb
####################↓ Display the products saved in the DB ↓####################
<ul class='item-lists'>
<% if @items %>
<% @items.each do |item| %>
<li class='list' >
<%= link_to item_path(item.user_id) do %>
<div class='item-img-content'>
</div>
<div class='item-info'>
<h3 class='item-name'>
<%= item.name %>
</h3>
<div class='item-price'>
<span><%= item.price %>Circle<br>(tax included)</span>
<div class='star-btn'>
<%= image_tag "star.png ", class:"star-icon" %>
<span class='star-count'>0</span>
</div>
</div>
</div>
<% end %>
</li>
<% end %>
####################↓ The following are dummy products ↓####################
<% else %>
<li class='list'>
<%= link_to '#' do %>
<%= image_tag "https://s3-ap-northeast-1.amazonaws.com/mercarimaster/uploads/captured_image/content/10/a004.png ", class: "item-img" %>
<div class='item-info'>
<h3 class='item-name'>
Selling goods
</h3>
<div class='item-price'>
<span>0 Yen<br>(tax included)</span>
<div class='star-btn'>
<%= image_tag "star.png ", class:"star-icon" %>
<span class='star-count'>0</span>
</div>
</div>
</div>
<% end %>
</li>
<% end %>
</ul>
</div>
"In short, if the DB is empty, the instance variables will also be empty, right?" It was working based on the idea
I was at that time
I remember being obsessed with the word If only that is suppressed ...! I was convinced that it was too mysterious when I think about it now
After an hour, I finally had some doubts in myself. "... Maybe just because the table is empty doesn't mean that the contents of what you generate will be empty, right?" And the contents of params
An empty value can be entered, and even if the content is completely empty, it will not be recognized as empty when the array exists.
I have to prove that it's an empty value, but it's not empty
I asked the instructor for help The answer returned is
"If the contents of an instance variable are empty and you want to return false, but an array is created, why not judge whether the number of elements in the array is 0?"
It was
In other words, like this!
ruby:index.html.erb
<ul class='item-lists'>
<% if @items.length != 0 %> #← Here!
<% @items.each do |item| %>
<li class='list' >
<%= link_to item_path(item.user_id) do %>
#The following is omitted
In other words
something like By passing the number of arrays in .length and specifying! = 0 If it is not 0, true will be returned.
There was a clear and refreshing understanding because I was presented with something that I had never thought of. At the same time, there was also the regret that I thought, "Did you notice it?"
The lesson learned this time is not related to one idea To see things from a more bird's-eye view