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

sinatra nested form practice #1

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
32 changes: 30 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
require './environment'

require 'pry'
module FormsLab
class App < Sinatra::Base

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

get '/new' do
erb :new
end

post '/pirates' do

@pirate = Pirate.new
@pirate.name = params[:pirate][:name]
@pirate.weight = params[:pirate][:weight]
@pirate.height = params[:pirate][:height]

params[:pirate][:ships].each do |data_hash|
Ship.new(data_hash)
end
@ships = Ship.all

# # @ship_1.booty = params[:pirate][:ships][][:booty]
# # @ship_2.name = params[:pirate][:ships][][:name]
# # @ship_2.type = params[:pirate][:ships][][:type]
# # @ship_2.booty = params[:pirate][:ships][][:booty]
#use each_with_index on params[:pirate][:ships] and use Ship.All to view the created ships and assign them variables.


erb :show
end

end
end
13 changes: 12 additions & 1 deletion app/models/pirate.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
class Pirate
end
attr_accessor :name, :weight, :height
@@all = []

def initialize
@@all << self
end

def self.all
@@all
end

end
19 changes: 18 additions & 1 deletion app/models/ship.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
class Ship
end
attr_accessor :name, :type, :booty
@@all = []
def initialize(args)
@name = args[:name]
@type = args[:type]
@booty = args[:booty]
@@all << self
end

def self.all
@@all
end

def self.clear
@@all.clear
end

end
Binary file added views/.DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions views/new.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Basketball Team Signup</title>
</head>
<body>
<h1>Make your form here</h1>
<form method="post" action="/pirates">
<p>Name:<input type="text" name="pirate[name]"></p>
<p>Weight:<input type="text" name="pirate[weight]"></p>
<p>Height: <input type="text" name="pirate[height]"></p>

<p>Name: <input id = "ship_name_1" type="text" name="pirate[ships][][name]"></p>
<p>Type: <input id = "ship_type_1" type="text" name="pirate[ships][][type]"></p>
<p>Booty: <input id = "ship_booty_1" type="text" name="pirate[ships][][booty]"></p>

<p>Name: <input id = "ship_name_2" type="text" name="pirate[ships][][name]"></p>
<p>Type: <input id = "ship_type_2" type="text" name="pirate[ships][][type]"></p>
<p>Booty: <input id = "ship_booty_2" type="text" name="pirate[ships][][booty]"></p>
<input type="submit" id = "Submit">
</form>
</body>
</html>
1 change: 0 additions & 1 deletion views/pirates/new.erb

This file was deleted.

7 changes: 0 additions & 7 deletions views/pirates/show.erb

This file was deleted.

25 changes: 25 additions & 0 deletions views/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Display your Pirate here</h1>
<p><%= @pirate.name %></p>
<p><%= @pirate.weight %></p>
<p><%= @pirate.height %></p>

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


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