I am creating a cinema Ruby on Rails application and am currently working on the bookings.
A user can select the amount of adult seats, child seats, student seats, and senior seats. A screen has many seats that are stored in the seats table but the user is not booking a specific seat but a quantity of seats. The drop down menus allow the user to select a maximum of 10 seats for each quantity (adult, child, student, senior) but if the screen has 50 seats and 45 of them are booked then the drop down menus are limited to a maximum of 5. But my problem is that this maximum is on each drop down - so a user could select 5 adult seats, child seats, student seats, and senior seats.
I know that I could add validation so that if a screen has 50 seats but 45 have already been booked and the user selects 5 adults, 5 children, 5 student, and 5 senior seats then it will add the quantities and existing bookings (5 + 5 + 5 + 5 + total existing bookings = 65) to ensure this is not over the screen's capacity. But what I want to do is make it so that the dropdown menus update based on the previous selection. So if there are a total of 5 seats left to book, and a user selects 3 adult seats the child, student, and senior dropdowns will be restricted to a maximum of 2.
Can someone please help me to do this. I have looked at so many links for dynamic drop downs but none of them address anything like this as they focus on things like countries and states.
views/bookings/_form.html.erb:
<td>
<%= f.select :adult_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s %><br>
</td>
<td>
<%= f.select :child_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s %><br>
</td>
<td>
<%= f.select :senior_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s %><br>
</td>
<td>
<%= f.select :student_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s %><br>
</td>
Screen.rb:
class Screen < ActiveRecord::Base
has_many :seats
has_many :showings
has_many :bookings, through: :showings
def remaining_seats
available_seats = seats.count - bookings.sum(:adult_seats)
available_seats = available_seats - bookings.sum(:child_seats)
available_seats = available_seats - bookings.sum(:disabled_seats)
available_seats = available_seats - bookings.sum(:student_seats)
available_seats = available_seats - bookings.sum(:senior_seats)
available_seats = available_seats - bookings.sum(:immortal_seats)
end
end
Schema.rb:
create_table "bookings", force: :cascade do |t|
t.integer "user_id"
t.integer "showing_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "adult_seats"
t.integer "child_seats"
t.integer "senior_seats"
t.integer "student_seats"
end
create_table "screens", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "seats", force: :cascade do |t|
t.string "row_letter"
t.integer "row_number"
t.integer "screen_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "showings", force: :cascade do |t|
t.date "show_date"
t.time "show_time"
t.integer "film_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "screen_id"
end
Aucun commentaire:
Enregistrer un commentaire