Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, April 07, 2010

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

image

 

image

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 %>

image

We also need a partial for the seat list

image

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

image

another custom validator in the seat model

if flight.seats.size >= flight.capacity
            errors.add_to_base("The flight is fully booked")
        end

image

**TO DO put in a validator that checks there is a number for baggage

All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview