ruby script/generate scaffold flight departure:datetime arrival:datetime destination:string baggage_allowance:decimal capacity:integer
ruby script/generate scaffold seat flight_id:integer name:string baggage:decimal
rake db:migrate
What we want is a form that has the flight info on it, then the ability to add a new seat booking.
Partials
Coppied seat/new.html.erb into flight/show.html.erb file rendering as a partial
_new_seat.html.erb
Changed:
<% form_for(seat) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :flight_id %><br />
<%= f.text_field :flight_id %>
</p>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :baggage %><br />
<%= f.text_field :baggage %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
the form_for(@seat) to (seat) to become a local variable to the partial. This is good practise.
called the partial from flights/show.html.erb as
<%= render :partial=>"new_seat", :locals=>{:seat=>Seat.new(:flight_id=>@flight.id)}%>
<% form_for(seat) do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :flight_id %>
We also need a partial for the seat list
however this is bad as its displaying the seats for all flights
Connect the models together
means we don’t need to use finders.. it works automatically.
class Flight < ActiveRecord::Base
has_many :seats
end
then when rendering out the listing of seats we can pass in the array in show.html.erb
<%= render :partial=>"seat_list", :locals=>{:seats=> @flight.seats}%>
in the model:
belongs_to :flight
def validate
if baggage > Flight.find(flight_id).baggage_allowance
errors.add_to_base("You have too much baggage")
end
if flight.seats.size >= flight.capacity
errors.add_to_base("The flight is fully booked")
end
end

another custom validator in the seat model
if flight.seats.size >= flight.capacity
errors.add_to_base("The flight is fully booked")
end
**TO DO put in a validator that checks there is a number for baggage