mercredi 25 mars 2015

Ruby on Rails Update Price When User Selects Value from Drop Down

I have a cinema system in Ruby on Rails and am currently working on the booking functionality. When a user makes a booking they can choose the number of adult, child, senior, and student seats from a drop down menu:



<%= f.select :adult_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %>
<%= f.select :child_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %>
<%= f.select :senior_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %>
<%= f.select :student_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %>


What I want to be able to do is have a text box at the bottom of the page that will update the total price based on the user's select. So if they select 2 adult seats the text would say 20.00, the difficult part is that the prices are in a separate table, so I need a method of retrieving the prices and then updating the text.


This is my full views/bookings/_form.html.erb:



<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<div id="contentWrapper">
<div id="content">
<div class='large_panel'>
<table>
<form id="prices">
<%= form_for @booking do |f| %>
<%= f.hidden_field :user_id %>
<%= f.hidden_field :showing_id %>
<%#= hide_action @booking.showing.screen.remaining_seats %>
<%= image_tag @booking.showing.film.image_url,:size => "900x250" %>
<h1>NEW BOOKING:</h1>
<tr>
<td width="280px"></td>
<td width="100px">
<br><%= f.label :adult_seats, 'Adults:' %>
</td>
<td width="100px">
<br><%= f.label :child_seats, 'Children:' %>
</td>
<td width="100px">
<br><%= f.label :senior_seats, 'Seniors:' %>
</td>
<td width="100px">
<br><%= f.label :student_seats, 'Students:' %>
</td>
<td width="100px">

</td>
</tr>
<tr>
<td>
How many seats do you require:
</td>
<td>
<input type="hidden" value="#{@booking.showing.screen.remaining_seats}" name="seats_available" id="seats_available">
<%= f.select :adult_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %><br>
</td>
<td>
<%= f.select :child_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %><br>
</td>
<td>
<%= f.select :senior_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %><br>
</td>
<td>
<%= f.select :student_seats, '0'..(@booking.showing.screen.remaining_seats > 10 ? 10 : @booking.showing.screen.remaining_seats).to_s, id: 'Seats' %><br>
</td>
<td>
<div class="actions">
<%= f.submit 'Book Showing' %>
</div>
</td>
</tr>
<% end %>
</form>
</table><br><%= @booking.showing.screen.screens_info %> <%= @booking.film_age_rating %>
</div><%= render "/error_messages", :message_header => "Cannot save: ", :target => @booking %>
</div>
</div>


And the schema:



create_table "prices", force: :cascade do |t|
t.text "name"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

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"
t.integer "disabled_seats"
t.integer "immortal_seats"
end


Can anyone help.


Aucun commentaire:

Enregistrer un commentaire