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

Display Nested Form Data #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Display show page
  • Loading branch information
ASPhillips8 committed Aug 8, 2024
commit 2c8c4a3bcc4663b23f70def9db1c373c52ba03a0
12 changes: 12 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@ class App < Sinatra::Base
erb :'pirates/new'
end

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this would become very simple:

Suggested change
@pirate = Pirate.new(params[:pirate][:name], params[:pirate][:weight], params[:pirate][:height])
@pirate = Pirate.new(params[:pirate])


params[:pirate][:ships].each do |details|
Ship.new(details)
end

@ships = Ship.all

erb :'pirates/show'
end

end
end
8 changes: 4 additions & 4 deletions app/models/ship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class Ship

@@ships = []

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

Expand Down
9 changes: 5 additions & 4 deletions views/pirates/new.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
<h2>Ship 1</h2>
<label>Name</label>
<input type="text" id="ship_name_1" name="pirate[ships][][name]">
<label>Weigth</label>
<label>Type</label>
<input type="text" id="ship_type_1" name="pirate[ships][][type]">
<label>Height</label>
<label>Booty</label>
<input type="text" id="ship_booty_1" name="pirate[ships][][booty]">
<h2>Ship 2</h2>
<label>Name</label>
<input type="text" id="ship_name_2" name="pirate[ships][][name]">
<label>Weigth</label>
<label>Type</label>
<input type="text" id="ship_type_2" name="pirate[ships][][type]">
<label>Height</label>
<label>Booty</label>
<input type="text" id="ship_booty_2" name="pirate[ships][][booty]">
<input type="submit" id="submit" value="Submit">

</form>
13 changes: 8 additions & 5 deletions views/pirates/show.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<h1>Display your Pirate here</h1>
<h1>Pirates</h1>
<h2>Name: <%[email protected] %></h2>
<p>Height: <%[email protected] %> Weight: <%[email protected] %></p>


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


<h2>Display your second ship here</h2>
<h2>Ships</h2>
<% @ships.each do |ship| %>
<h3>Name: <%=ship.name%></h3>
<p>Type: <%=ship.type%> Booty: <%=ship.booty%></p>
<%end%>