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

I liked this lab, especially with all the booty #5

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
23 changes: 23 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ module FormsLab
class App < Sinatra::Base

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

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

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

params[:pirate][:ships].each do |ship|
Ship.new(ship[:name], ship[:type], ship[:booty])
end

@ships = Ship.all

erb :'pirates/show'
end

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

end
end
18 changes: 17 additions & 1 deletion app/models/pirate.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
class Pirate
end

attr_accessor :name, :weight, :height

@@all = []

def initialize(name, weight, height)
@name = name
@weight = weight
@height = height
@@all << self
end

def self.all
@@all
end

end
22 changes: 21 additions & 1 deletion app/models/ship.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
class Ship
end

attr_accessor :name, :type, :booty

@@all = []

def initialize(name, type, booty)
@name = name
@type = type
@booty = booty
@@all << self
end

def self.all
@@all
end

def self.clear
@@all.clear
end

end
2 changes: 2 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Welcome to the Nested Forms Lab!</h1>
<p><a href="/new">let's navigate to the '/new'</a></p>
15 changes: 14 additions & 1 deletion views/pirates/new.erb
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
<h1>Make your form here</h1>
<h1>New Pirate and Ship Listings</h1>

<form method="POST" action="/pirates">
<p>Pirate Name: <input type="text" name="pirate[name]"><br>
Pirate Weight: <input type="text" name="pirate[weight]"><br>
Pirate Height: <input type="text" name="pirate[height]"></p>
<p>Ship 1 Name: <input id="ship_name_1" type="text" name="pirate[ships][][name]"><br>
Ship 1 Type: <input id="ship_type_1" type="text" name="pirate[ships][][type]"><br>
Ship 1 Booty: <input id="ship_booty_1" type="text" name="pirate[ships][][booty]"></p>
<p>Ship 2 Name: <input id="ship_name_2" type="text" name="pirate[ships][][name]"><br>
Ship 2 Type: <input id="ship_type_2" type="text" name="pirate[ships][][type]"><br>
Ship 2 Booty: <input id="ship_booty_2" type="text" name="pirate[ships][][booty]"></p>
<button type="submit" value="submit">Submit</button>
</form>
18 changes: 11 additions & 7 deletions views/pirates/show.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<h1>Display your Pirate here</h1>


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


<h2>Display your second ship here</h2>
<h1>Pirate:</h1>
<h2><%= @pirate.name %></h2>
<p><%= @pirate.weight %><br>
<%= @pirate.height %></p>
<br>
<h1>Ships:</h1>
<% @ships.each do |ship| %>
<h2><%= ship.name %></h2>
<p><%= ship.type %><br>
<%= ship.booty %></p>
<% end %>