vendredi 3 juin 2016

Create multiple records with checkbox in has_many association

I'm trying to create multiple objects, all with the same showing_id and booking_id params but with diffirent seat_ids which depends on selected checkboxes.

Reservedseats model:

class Reservedseat < ActiveRecord::Base
belongs_to :showing
belongs_to :seat
belongs_to :booking

end

the form:

<%= form_for([@booking,@reservedseat]) do |f| %>
<% @reservedseat.booking.showing.screen.seats.order(row: :asc,number::asc).each do |seat| %>
    (<%= seat.row %>:<%= seat.number %>)
    <%= check_box_tag :seat_id, seat.id %>
<% end %>
<%= f.hidden_field :booking_id, :value => @booking_id %>
<%= f.hidden_field :showing_id, :value => @booking.showing_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

The idea was to create a booking with name, e-mail etc and pass the params to reservedseats form for selecting multiple seats

current create method:

def create
@booking = Booking.find(params[:booking_id])
@showing_id = @booking.showing_id
@reservedseat = @booking.reservedseats.build(params[:reservedseat])




Aucun commentaire:

Enregistrer un commentaire