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

Done #6

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
21 changes: 21 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ 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

# extracts the form data from the params and uses it to create a new instance of your model class, something along the lines of Model.create(some_attribute: params[:some_attribute])
post '/pirates' do
@pirate = Pirate.new(params[:pirate])
params[:pirate][:ships].each { |ship| Ship.new(ship) }
@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

@@all = []

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

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

@@all = []

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

def self.all
@@all
end
end
2 changes: 1 addition & 1 deletion spec/01_sinatra_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
end

after do
Ship.clear
Ship.all.clear
end

it "returns a 200 status code" do
Expand Down
56 changes: 55 additions & 1 deletion views/pirates/new.erb
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
<h1>Make your form here</h1>
<h1>Create a Pirate</h1>

<form action="/pirates" method="POST">
<!-- pirate[name] -->
<label for="pirate_name">Pirate Name: </label>
<input type="text" name="pirate[name]" id="pirate_name">
<br>

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

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

<!-- ship_1[name] -->
<label for="ship_name_1">Ship 1 Name: </label>
<input type="text" name="pirate[ships][][name]" id="ship_name_1">
<br>

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

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

<!-- ship_2[name] -->
<label for="ship_name_2">Ship 2 Name: </label>
<input type="text" name="pirate[ships][][name]" id="ship_name_2">
<br>

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

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

<input type="submit" value="Submit">


</form>
12 changes: 9 additions & 3 deletions views/pirates/show.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<h1>Display your Pirate here</h1>
<h1><%= @pirate.name %></h1>
<h1><%= @pirate.weight %></h1>
<h1><%= @pirate.height %></h1>


<h2>Display your first ship here</h2>
<h2><%= @ships.first.name %></h2>
<h2><%= @ships.first.type %></h2>
<h2><%= @ships.first.booty %></h2>

<h2><%= @ships.last.name %></h2>
<h2><%= @ships.last.type %></h2>
<h2><%= @ships.last.booty %></h2>

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