In this lab, you'll practice using nested forms in Sinatra.
Note: YOU DO NOT NEED A DATABASE, since we just need to keep track of the forms input long enough to display it, and not persist it. We don't expect the pirate to be there the next time we come to their URL, but it'd be cool if they were.
-
Create two classes, a
Pirate
class and aShip
class. Pirates should have a name, weight, and height. You will also need a class method that returns all the pirates. The ship class should have name, type, and booty attributes, as well as a class method.all
that returns all the ships and a class method.clear
that deletes all ships. -
Make a nested form (this should probably have html
<label>
s so it makes sense to a user). This form will be creating three objects (one instance of the Pirate class, and two instances of the Ship class). Remember, you'll need to build a corresponding controller action to load this page. Your form shouldPOST
to the route'/pirates'
. -
After a user clicks submit they should be taken to a page that displays all the information we just posted from the form. You should have a page that shows the pirate you created along with their ships and all the details about the pirate and their ships.
Pass the tests! You'll notice in specs that we use Capybara to fill in certain fields. It looks something like this:
fill_in("ship_name_1", :with => "Flying Dutchman")
fill_in("ship_type_1", :with => "ghost ship")
fill_in("ship_booty_1", :with => "gold coins")
The word in quotes after fill_in needs to be set as an ID in the form. This is because your ship name one and two will have the same name
property. Capybara needs some sort of unique indentifier, so we use id
. So something like this:
<input id="ship_name_1" type="text" name="pirate[ships][][name]" >
View Sinatra Nested Forms Lab: Pirates! on Learn.co and start learning to code for free.