Hello, there are 100 div blocks on the page (each block is a place in the cinema). If several blocks are selected, an array is created in which the block id is written and sent to the ruby ​​variable. Further, the form for adding records to the database is rendered.

<% if @test_test.present? %> <% @test_test.each do |i| %> <div class="col-md-6 booking_blocks" id="<%= i %>"> <button class="close" id="close" value="<%= i %>">×</button> <%= form_for(@place) do |f| %> <div class="field"> <%= f.hidden_field :cinema_id, :value => @film_session.cinema_id %> <%= f.label :cinema_name %><br> <p><%= @cinema.cinema_name %></p> </div> <%= f.hidden_field :film_session_id, :value => @film_session.id %> <%= f.hidden_field :place_number, :value => i %> <div class="field"> <%= f.label :session_name %><br> <p><%= @film_session.session_name %></p> </div> <div class="field"> <%= f.label :place_number %><br> <p><%= i %></p> </div> <div class="field"> <%= f.label :status %><br> <%= f.check_box :status, class:"form form-control" %> </div> <br/> <% end %> </div> <% end %> <div class="actions"> <%= submit_tag "Booking", :class => "btn btn-primary" %> </div> 

The form displays data from the selected blocks. enter image description here

Please tell me how to implement a record in the database of several objects from this form at once by pressing a single submit button.

    1 answer 1

    It is not very clear what exactly should happen. But I would not make many forms. As far as I understand, cinema_id and film_session_id will be the same in all these forms. Only place_number and status place_number . If you look closely, it will become clear that we do not need both of these fields in the request. It is enough to have place_number arrays. Thus, we get an approximate structure of the form:

     <%= form_for(@booking) do |form| %> <%= form.hidden_field :cinema_id %> <%= form.hidden_field :film_session_id %> <%= form.collection_check_boxes :place_numbers, @test_test, :dup, nil %> ... <% end %> 

    The structure is very approximate. For example, instead of @test_test , where, as I understand it, it’s just an array of numbers, I would advise using something more meaningful.

    Well, now for this form we need an object.

     class BookingForm include ActiveModel::Validations include ActiveModel::Conversion include Virtus.model attribute :cinema_id attribute :film_session_id attribute :place_numbers validates :place_numbers, presence: true # должно быть выбрано хотябы одно место def save return false unless valid? # Здесь пишем код, который должен разложить данный по моделям end def persisted? false #только для новой брони end end 

    Then in the controller we write something like

     def new @booking = BookingForm.new(cinema_id: @film_session.cinema_id, film_session_id: @film_session.id) end def create @booking = BookingForm.new(params.require(:booking_form).permit(:cinema_id, :film_session_id, place_numbers: []) if @booking.save redirect_to action: :index else render action: :new end end 

    Once again, this is just an example. I do not know your task completely. Most likely, it can and should be optimized. And just need to add.

    I recommend to read the book Growing Rails Applications in Practice .

    • thanks for the answer, I will understand, but with cinema_id and film_session_id you are a little mistaken) Specifically for this cinema and for this cinema session these values ​​will be the same, but there may be several cinemas and tens cinema sessions. The data is entered in order to distinguish at what session and in which cinema the place is reserved. - Maxim Cherevatov
    • @MaximCherevatov, I mean, can a user send a request with several cinema_id and film_session_id ? Just in my opinion, the reservation (is this a reservation?) Is going on for a specific session and a particular cinema. To book seats in another room - go to the page of that room. If this approach is wrong, I'm afraid my answer is completely wrong :(. - anoam
    • No, you understood correctly! Thank you) - Maxim Cherevatov