What I want to solve
We are building a reservation system. I would like to be able to check on the screen whether a reservation is available or not. The reservation model is used for the association, and the reserved ID is managed by sheet_id. We will change the color of the sheet_id in the reservation model to make it easier to see if it is available for reservation.
Problem
I have color-coded the sheets and unavailable in the View, but they all have the same color. If there is no value in the reservation table, we want the color to be gray.
#Code
View
<body>
<div class="screen">
<p>スクリーン</p>
</div>
<table>
----------------------------------------------------------------------------------------
# The sheet contains the master data.
<% @sheets.each do |sheet|%>
<%= link_to new_movie_schedule_reservation_path(movie_id: @movie_id, schedule_id: @schedule_id,date: @date,sheet_id: sheet.id) do %>
# Use conditional branching to split the output between true and false.
<% if reservation_list(sheet.id) %>
<div class="sheet">
<p><%= sheet.row %><%= sheet.column %></p>
</div>
<% else %>
<div class="unavailable">
<p><%= sheet.row %><%= sheet.column %></p>
</div>
<% end %>
<% end %>
<% end %>
----------------------------------------------------------------------------------------
</table>
</body>
helper
# The idea was to use the ID as an argument to separate the process if it is in the reservation table and if it is not.
def reservation_list(sheet)
list = Reservation.where("sheet_id LIKE ?", "%#{sheet}%")
if list.nil?
false
else
true
end
end
end
What I've tried.
・Used helper, tried to change the process if the same thing was found by searching in the reservations table.
def reservation_list(sheet)
list = Reservation.where("sheet_id LIKE ?", "%#{sheet}%")
if list.nil?
false
else
true
end
end
・Separate output using if in View.
<% if reservation_list(sheet.id) %>
・・・・
<% else %>
・・・・
<% end %>
