This problem is about loading Packages onto Trucks and validating that the Truck can hold the Packages.
- run
rails new cargo
to create a new Rails app - implement a model, Package, with a
size
attribute- expect
Package.create!(size: 10)
to create and store an instance of Package
- expect
- implement a model, Truck, that has a
capacity
attribute and many Packages (a Package may be on a Truck, or it may not)- expect
Truck.create!(capacity: 35)
to create and store an instance of Truck - expect
Package.first.update!(truck: Truck.first)
to associate the Package with the Truck - expect
Truck.first.packages
to return the Package that was added to the Truck - expect
Package.create!(size: 5)
to create and store an instance of Package
- expect
- add an instance method on Truck called
load_cargo
that takes a Package and "puts it on the truck"- expect
Truck.first.load_cargo(Package.last)
to run without error - expect
Truck.first.packages
to return two Packages, including the Package that was passed toTruck#load_cargo
- expect
- add validation to ensure that the sum of the
size
s of its Packages does not exceed a Truck'scapacity
- expect
Package.create!(size: 20) && Truck.first.load_cargo(Package.last)
to create and load_cargo a Package - expect
Package.create!(size: 1) && Truck.first.load_cargo(Package.last)
to create an instance of Package but fail to save the Truck
- expect
- start from the git tag
controllers
- if you have data in your database, stop the server and run
rails db:reset
- if you don't have data in your database, run
rails db:setup
- if you have data in your database, stop the server and run
- create some model instances
# first Truck
truck = Truck.create!(capacity: 35)
Package.create!(size: 10, truck: truck)
Package.create!(size: 5, truck: truck)
Package.create!(size: 20, truck: truck)
# second Truck
truck = Truck.create!(capacity: 50)
Package.create!(size: 20, truck: truck)
Package.create!(size: 20, truck: truck)
Package.create!(size: 2, truck: truck)
# third Truck
truck = Truck.create!(capacity: 10)
- add a Controller for the Truck model, with the actions
index
andshow
, and implement a View for theindex
action that lists the Trucks from the database, showing the attributesid
,capacity
, andcargo_size
for each Truck- expect http://localhost:3000/trucks/index to render the view and show the three Trucks created in the Setup section
- make each
id
attribute in theindex
page a link to the Truck'sshow
page- expect clicking on an id in the
index
page to take the user to the emptyshow
page
- expect clicking on an id in the
- implement the
show
page, to show the same three attributes as the index page- expect the
show
page to show the attributes
- expect the
- on the
show
page, list the ID of each Package on the Truck- expect the
show
page to show the ID of each Package on the Truck
- expect the
- add a Controller for the Package model, with the
show
action, and implement a View forPackages#show
that displays the Package attributesid
andsize
- expect loading
/packages/1
to show the Package withid == 1
- expect loading
- make each Package
id
on theTrucks#show
page a link to that Package'sshow
page- expect clicking a Package link to take the user to the Package's
show
page
- expect clicking a Package link to take the user to the Package's
- add a JSON response to the
Trucks#show
action- expect
curl -H 'Accept: application/json' http://localhost:3000/trucks/1
to return a JSON hash
- expect
- have the Packages controller return a 403 Forbidden response if the Package ID is odd (i.e. not an even number)
- expect
/packages/3
to show an error message and not show the package information - expect
curl -H 'Accept: application/json' http://localhost:3000/packages/3
to return status code 403 and an error message, and not the package information
- expect