Skip to content

Commit

Permalink
allows a user to add items
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshamann committed Jun 11, 2017
1 parent fba8db4 commit 5471f5c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
22 changes: 22 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,26 @@ def index
@items = Item.all
end

def new
@item = Item.new
end

def create
@item = Item.create(item_params)
if @item.save
redirect_to items_path
flash[:notice] = "Item successfully added!"
else
render "new"
flash[:alert] = "Sorry, you must add a valid item"
end
end

private

def item_params
params.require(:item).permit(:name, :description, :price)
end


end
12 changes: 9 additions & 3 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<div class="container">
<h2>Items</h2>

<% if @items.any? %>
<% @items.each do |i| %>
<p> <%= i.name %></p>
<p> <%= i.description %></p>
<% end %>
<% else %>
<h2>No items yet!</h2>
<% end %>
No items available

<%= link_to "Add Item" %>
<%= link_to "Add a new Item", new_item_path %>


</div>
8 changes: 8 additions & 0 deletions app/views/items/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Adding Items</h1>

<%= form_for(@item, html: {multipart: true}) do |f| %>
Name: <%= f.text_field :name %>
Description: <%= f.text_field :description %>
Price: <%= f.number_field :price %>
<p><%= f.submit %></p>
<% end %>
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"covered_percent": 100.0
"covered_percent": 97.75
}
}
36 changes: 35 additions & 1 deletion coverage/.resultset.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
1,
null,
null,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
null,
null,
null
],
"/Users/jameshamann/Documents/Dev_Work/webshop/spec/rails_helper.rb": [
Expand Down Expand Up @@ -296,7 +308,29 @@
1,
null,
1,
3,
null,
null,
1,
1,
null,
null,
1,
1,
1,
1,
1,
null,
0,
0,
null,
null,
null,
1,
null,
1,
1,
null,
null,
null,
null
Expand All @@ -317,6 +351,6 @@
null
]
},
"timestamp": 1496953996
"timestamp": 1497205308
}
}
16 changes: 14 additions & 2 deletions spec/features/adding_an_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@

it 'Shows no Items are available and allows a user to add items' do
visit '/items'
expect(page).to have_content("No items available")
expect(page).to have_link("Add Item")
expect(page).to have_content("No items yet!")
expect(page).to have_link("Add a new Item")
end

it 'Allows a user to add items' do
visit '/items'
click_link ("Add a new Item")
expect(current_path).to eq "/items/new"
fill_in 'item_name', with: 'Sample Kit'
fill_in 'item_description', with: 'A Sampling Kit to be used for sample analysis'
fill_in 'item_price', with: '10'
click_button 'Create Item'
expect(current_path).to eq '/items'
expect(page).to have_content 'Sample Kit'
end
end
end

0 comments on commit 5471f5c

Please sign in to comment.