vendredi 20 février 2015

4 Drop down menus, update each one based on the selection before to SEARCH


I am creating a cinema application in Ruby on Rails and am currently working on the bookings.


What I am trying to do is implement a search so that a user can search for a free seat, select that seat, and then book that seat for a film's showing.


To do this, I want to implement 4 drop down menus: films, showings, screens and seats. But the difficulty is that what I want is the drop down menus to be dynamic and update themselves based on the previous selection, so when a user selects a film, the showing drop down only shows showings for that film, when the showing is selected the screen drop down only shows the screen that showing is in, and then the screen drop down menu is used so that the seats drop down only shows seats that are in that screen.


I have so far enabled the user to search for a screen and then when they click search the seats available are shown on the seats/index.html.erb.


This was done using the partial _booking_lookup.html.erb:



<%= form_tag my_path, :method=>'post', :multipart => true do %>

<%= select_tag ('screen_id'),
options_from_collection_for_select(@screens, :id, :screens_info, 0 ),
:prompt => "Screen" %>


<%= submit_tag 'Search' %>
<% end %>


Which is displayed on the layouts/application.html.erb:



<%= render(:partial => '/booking_lookup', :locals=> {:film => @films = Film.all, :showings => @showings = Showing.all, :screens => @screens = Screen.all, :seats => @seats = Seat.all, :my_path => '/seats/display_seats_by_screen' }) %>


And links to the display_seats_by_screen in the seats_controller:



def display_seats_by_screen
@seats = Seat.screen_search(params[:screen_id])
if @seats.empty?
# assign a warning message to the flash hash to be displayed in
# the div "feedback-top"
flash.now[:alert] = "There are no films of that genre."
# return all products, in alphabetical order
@seats = Seat.all
end
render :action => "index"
end


Which uses the screen_search in the seat.rb model:



def self.screen_search(search_string)
self.where("screen_id = ?", search_string)
end


So essentially for the above before the user selects the screen they should have selected a showing and before that a film so that only screens for that showing are shown so they can select an appropriate seat.


The models associations: film:



has_many :showings
belongs_to :certificate
belongs_to :category


showing:



belongs_to :film
has_many :bookings
belongs_to :screen


screen:



has_many :seats


seat:



belongs_to :screen
has_many :bookings


Can anyone help?





Aucun commentaire:

Enregistrer un commentaire