Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Sinatra Nested Forms #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,30 @@
module FormsLab
class App < Sinatra::Base

# code other routes/actions here
get '/' do
erb :root
end

get '/new' do
erb :'pirates/new'
end

get 'pirates' do
erb :'pirates/show'
end

post '/pirates' do
@pirate = Pirate.new(params[:pirate])

params[:pirate][:ships].each do |details|
Ship.new(details)
end

@ships = Ship.all

erb :'pirates/show'
end


end
end
16 changes: 15 additions & 1 deletion app/models/pirate.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
class Pirate
end
attr_accessor :name, :weight, :height

PIRATES = []

def initialize(params)
@name = params[:name]
@weight = params[:weight]
@height = params[:height]
PIRATES << self
end

def self.all
PIRATES
end
end
20 changes: 19 additions & 1 deletion app/models/ship.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
class Ship
end
attr_accessor :name, :type, :booty

SHIPS = []

def initialize(params)
@name = params[:name]
@type = params[:type]
@booty = params[:booty]
SHIPS << self
end

def self.all
SHIPS
end

def self.clear
SHIPS.clear
end
end
44 changes: 44 additions & 0 deletions views/pirates/new.erb
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
<h1>Make your form here</h1>
<form action="/pirates" method="post">
<h1> Pirate </h1>
<br>
<label for="pirate[name]">Pirate Name:</label>
<input type="text" id="pirate[name]" name="pirate[name]">
<br><br>

<label for="pirate[height]">Pirate Height:</label>
<input type="text" id="pirate[height]" name="pirate[height]">
<br><br>

<label for="pirate[weight]">Pirate Weight:</label>
<input type="text" id="pirate[weight]" name="pirate[weight]">
<br><br>

<h1> First Ship </h1>
<br>
<label for="ship_name_1">Ship Name:</label>
<input type="text" id="ship_name_1" name="pirate[ships][][name]">
<br><br>

<label for="ship_type_1">Ship Type:</label>
<input type="text" id="ship_type_1" name="pirate[ships][][type]">
<br><br>

<label for="ship_booty_1">Ship Booty:</label>
<input type="text" id="ship_booty_1" name="pirate[ships][][booty]">
<br><br>

<h1>Second Ship </h1>
<label for="ship_name_2">Ship Name:</label>
<input type="text" id="ship_name_2" name="pirate[ships][][name]">
<br><br>

<label for="ship_type_2">Ship Type:</label>
<input type="text" id="ship_type_2" name="pirate[ships][][type]">
<br><br>

<label for="ship_booty_2">Ship Booty:</label>
<input type="text" id="ship_booty_2" name="pirate[ships][][booty]">
<br><br>

<input id="submit" type="submit" value="Submit">
</form>
18 changes: 14 additions & 4 deletions views/pirates/show.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<h1>Display your Pirate here</h1>
<h1>Pirate</h1>

<div>
<h3>Pirate Name: <%= @pirate.name %></h3><br>
<h3>Pirate Height: <%= @pirate.height %></h3><br>
<h3>Pirate Weight: <%= @pirate.weight %></h3><br>
</div><br>

<h2>Display your first ship here</h2>


<h2>Display your second ship here</h2>
<h2>Display your ships here</h2>
<% @ships.each do |ship| %>
<div>
<p>Name: <%= ship.name %></p><br>
<p>Type: <%= ship.type %></p><br>
<p>Booty: <%= ship.booty %></p><br>
</div><br>
<% end %>