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

Pirate Forms #9

Open
wants to merge 2 commits 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
19 changes: 18 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@
module FormsLab
class App < Sinatra::Base

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

get '/new' do
erb :"pirates/new"
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
17 changes: 16 additions & 1 deletion app/models/pirate.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
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
21 changes: 20 additions & 1 deletion app/models/ship.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
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
14 changes: 13 additions & 1 deletion views/pirates/new.erb
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
<h1>Make your form here</h1>

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

<h1>Pirate</h1>

<h2>Display your first ship here</h2>
<h3>Name: <%= @pirate.name %></h3>
<h4>Weight: <%= @pirate.weight %></h4>
<h4>Height: <%= @pirate.height %></h4>
</div><br>

<h1>Ships</h1>
<h2>Head of the Fleet</h2>
<p><%= @ships[0].name%></p>
<p><%= @ships[0].type%></p>
<p><%= @ships[0].booty%></p>


<h2>Back up ship in case HoF goes DOWN</h2>
<p><%= @ships[1].name%></p>
<p><%= @ships[1].type%></p>
<p><%= @ships[1].booty%></p>

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