Skip to content
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
28 changes: 24 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
require './environment'
require_relative 'config/environment'
require 'sinatra/base'
require 'sinatra/reloader' if ENV['RACK_ENV'] != 'production'

module FormsLab
class App < Sinatra::Base
class App < Sinatra::Base
configure :development, :test do
register Sinatra::Reloader if defined?(Sinatra::Reloader)
also_reload '**/*.rb'
end

get '/' do
erb :root
end

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

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

# code other routes/actions here
params[:pirate][:ships].each do |details|
Ship.new(details)
end
@ships = Ship.all

erb :"pirates/show"
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_reader :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
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require './app'
run FormsLab::App
run App
2 changes: 1 addition & 1 deletion environment.rb → config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ENV['SINATRA_ENV'] ||= "development"
ENV['SINATRA_ENV'] ||= 'development'

require 'bundler'
Bundler.require(:default, ENV['SINATRA_ENV'])
Expand Down
42 changes: 42 additions & 0 deletions views/pirates/new.erb
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
<h1>Make your form here</h1>
<form action="/pirates" method="POST">
<div>
<label for="name">Name:</label><br>
<input type="text" id="name" name="pirate[name]"><br>

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

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

</div>

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

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

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

</div>

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

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

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

</div>

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

</form>
20 changes: 19 additions & 1 deletion views/pirates/show.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
<h1>Display your Pirate here</h1>
<h2>Pirate Name:</h2>
<p><%= @pirate.name %></p>

<h2>Pirate Weight:</h2>
<p><%= @pirate.weight %></p>

<h2>Display your first ship here</h2>
<h2>Pirate Height:</h2>
<p><%= @pirate.height %></p>

<h2>Display your first ship here</h2>
<% ship1 = @ships[0] %>
<% if ship1 %>
<p><%= ship1.name %></p>
<p><%= ship1.type %></p>
<p><%= ship1.booty %></p>
<% end %>

<h2>Display your second ship here</h2>
<% ship2 = @ships[1] %>
<% if ship2 %>
<p><%= ship2.name %></p>
<p><%= ship2.type %></p>
<p><%= ship2.booty %></p>
<% end %>