Hello, I have this cycle:

<% @session.number_of_session_places.times do |session| %> <% n = n+1 %> <div class="place_block" style=""><%= n %></div> <% @session.places.each do |p| %> <% if p.place_number == n %> <div class="place_block" style="background-color: black"><%= n %></div> <% end %> <% end %> <% end %> 

For obvious reasons, he displays the block first.

 <div class="place_block" style=""><%= n %></div> 

And then

 <div class="place_block" style="background-color: black"><%= n %></div> 

With the same number. The remaining entries are displayed without duplicates. Tell me how to solve this problem, I can not logically think out a solution.

Logic : There is a session in the cinema (@session), and there is a number of seats in the hall (number_of_session_places). The first cycle displays for example 100 seats in the hall. Next, there is a table in the database (places) that is associated with the session table (that is, only reserved places are added to the (places) table, for example, 21 with an indication of the session id). It turns out that the second cycle goes through all the reserved places for this session and displays a block with a black background, if this place is booked. The logic is simple, if a place is reserved - a block with a standard background is displayed, otherwise - a block with a black background.

    1 answer 1

    Your code first displays all the places, how many of them there are, including those reserved, and then displays only those reserved, with a different background, and that's duplication. Add a condition that displaying places with a regular background only if there is no armor, and you will be happy.

     <% @session.number_of_session_places.times do |session| %> <% n = n+1 %> <% @session.places.each do |p| %> <% if p.place_number == n %> # Если мСсто занято - Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ с Ρ„ΠΎΠ½ΠΎΠΌ <div class="place_block" style="background-color: black"><%= n %></div> <% else %> # Π˜Π½Π°Ρ‡Π΅ - Π±Π΅Π· <div class="place_block" style=""><%= n %></div> <% end %> <% end %> <% end %> 

    However, this is how you increase the page generation time: your code produces N*M interactions, where N is the number of seats, and M is the number of armor. Moreover, you cannot cache this part of the view, since it turns out to be dynamic, and this code will work every time the page is opened, which is unnecessary.

    It is better, for example, to assign a div id to each div 'place (optionally, it can also run over the children with the specified JS class):

     <div id="place_<%= n %>" class="place_block"><%= n %></div> 

    And the result of the work cycle is to cache.

    When you load the page, run JavaScript, receiving a POST request for the numbers of reserved seats and changing the class for them to some class="booked_place_block" , with the black background already set.

    Then you will only have N+M interactions when the user first enters the page and M on subsequent ones (the number of places you have will not change, only the number booked), which will increase the speed of your application, not to mention the possibility to hang up the armor check script by automatically updating the booked seats.

    If you do not use caching, you can implement the output by means of N + M interactions and on the server side, using the following logic developed during the brainstorming with the D-side :

     N=1 Π¦ΠΈΠΊΠ» 1: ΠŸΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ список Π·Π°Π±Ρ€ΠΎΠ½ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Ρ… мСст, отсортированный ΠΏΠΎ Π½ΠΎΠΌΠ΅Ρ€Π°ΠΌ мСст Π—Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Π±Ρ€ΠΎΠ½ΠΈ (M) Π¦ΠΈΠΊΠ» 2: Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π±Π»ΠΎΠΊΠΈ-мСста ΠΏΠΎ N, ΠΏΠΎΠΊΠ° N<M, Π·Π°Ρ‚Π΅ΠΌ Π²Ρ‹Ρ…ΠΎΠ΄ΠΈΠΌ ΠΈΠ· Ρ†ΠΈΠΊΠ»Π° 2 Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π±Ρ€ΠΎΠ½ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠ΅ мСсто N=M Π£Π²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ N Π½Π° 1, ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π°Ρ интСрация Ρ†ΠΈΠΊΠ»Π° 1 Π¦ΠΈΠΊΠ» 3: Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π±Π»ΠΎΠΊΠΈ-мСста ΠΏΠΎ N, ΠΎΡ‚ M+1 Π΄ΠΎ number_of_session_places, Π·Π°Ρ‚Π΅ΠΌ Π²Ρ‹Ρ…ΠΎΠ΄ΠΈΠΌ ΠΈΠ· Ρ†ΠΈΠΊΠ»Π° 3 
    • @Grundy In the above proposed version, all entries are displayed 2 times) But with JS, the idea is good. Thanks - Maxim Cherevatov
    • @Risto comments have nothing to do with it, I deleted them, but still all the records are displayed 2 times) - Maxim Cherevatov
    • one
      @MaximCherevatov means you output a block somewhere outside if a. - D-side
    • @Risto thanks! I will understand, while on JS I remake - Maxim Cherevatov