From 5ed7d8a8fe91b2b639ecc5acd02aa1fba7bb9e61 Mon Sep 17 00:00:00 2001 From: Nick Wohnhas Date: Fri, 29 Jun 2018 15:51:00 -0400 Subject: [PATCH] sinatra nested form practice --- app.rb | 32 ++++++++++++++++++++++++++++++-- app/models/pirate.rb | 13 ++++++++++++- app/models/ship.rb | 19 ++++++++++++++++++- views/.DS_Store | Bin 0 -> 6148 bytes views/new.erb | 24 ++++++++++++++++++++++++ views/pirates/new.erb | 1 - views/pirates/show.erb | 7 ------- views/show.erb | 25 +++++++++++++++++++++++++ 8 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 views/.DS_Store create mode 100644 views/new.erb delete mode 100644 views/pirates/new.erb delete mode 100644 views/pirates/show.erb create mode 100644 views/show.erb diff --git a/app.rb b/app.rb index 585554a..40f2914 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/app/models/pirate.rb b/app/models/pirate.rb index 80a578b..f3f5206 100644 --- a/app/models/pirate.rb +++ b/app/models/pirate.rb @@ -1,2 +1,13 @@ class Pirate -end \ No newline at end of file +attr_accessor :name, :weight, :height +@@all = [] + + def initialize + @@all << self + end + + def self.all + @@all + end + +end diff --git a/app/models/ship.rb b/app/models/ship.rb index 09d35d6..404ff0c 100644 --- a/app/models/ship.rb +++ b/app/models/ship.rb @@ -1,2 +1,19 @@ class Ship -end \ No newline at end of file +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 diff --git a/views/.DS_Store b/views/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 + + + + Basketball Team Signup + + +

Make your form here

+
+

Name:

+

Weight:

+

Height:

+ +

Name:

+

Type:

+

Booty:

+ +

Name:

+

Type:

+

Booty:

+ +
+ + diff --git a/views/pirates/new.erb b/views/pirates/new.erb deleted file mode 100644 index f407a19..0000000 --- a/views/pirates/new.erb +++ /dev/null @@ -1 +0,0 @@ -

Make your form here

diff --git a/views/pirates/show.erb b/views/pirates/show.erb deleted file mode 100644 index f7832d2..0000000 --- a/views/pirates/show.erb +++ /dev/null @@ -1,7 +0,0 @@ -

Display your Pirate here

- - -

Display your first ship here

- - -

Display your second ship here

diff --git a/views/show.erb b/views/show.erb new file mode 100644 index 0000000..2acb620 --- /dev/null +++ b/views/show.erb @@ -0,0 +1,25 @@ + + + + + + + +

Display your Pirate here

+

<%= @pirate.name %>

+

<%= @pirate.weight %>

+

<%= @pirate.height %>

+ +

Display your first ship here

+

<%= @ships[0].name%>

+

<%= @ships[0].type%>

+

<%= @ships[0].booty%>

+ + +

Display your second ship here

+

<%= @ships[1].name%>

+

<%= @ships[1].type%>

+

<%= @ships[1].booty%>

+ + +