Skip to content

Commit 37eb35b

Browse files
committed
Update project
1 parent ee69a48 commit 37eb35b

10 files changed

Lines changed: 146 additions & 48 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Like "Sneakers" for $40. You should see the success message we left in create.ht
154154
If you got an error, try to use the error messages to debug the problem.
155155

156156

157-
Go ahead and go back to [http://localhost:3000/products/new](http://localhost:3000/products/new). Enter in the name of a product that already exists, like "sneakers" and submit. You should be sent back to the form where we entered in the name and price for our product. This is because if `@product.save` does not return true in our controller we render the `new` action which has the form for our product. This is much more useful than simply saying the product didn't save like we did before, but we're leaving our user in the dark. Right now they don't know if that worked or not, and they don't know which fields didn't validate correctly. Even worse they lost everything they typed in to the form. This is a bad user experience for sure. Lets update this form to use Rails form helpers and give our users a useability boost in the next section.
157+
Go ahead and go back to [http://localhost:3000/products/new](http://localhost:3000/products/new). Enter in the name of a product that already exists, like "sneakers" and submit. You should be sent back to the form where we entered in the name and price for our product. This is because if `@product.save` does not return true in our controller we render the `new` action which has the form for our product. This is much more useful than simply saying the product didn't save like we did before, but we're leaving our user in the dark. Right now they don't know if that worked or not, and they don't know which fields didn't validate correctly. Even worse they lost everything they typed in to the form. This is a bad user experience for sure. Lets update this form to use Rails form helpers and give our users a usability boost in the next section.
158158

159159
Save and commit the results to GIT.
160160

@@ -379,7 +379,7 @@ First we will be making an edit view. The edit view is what you see before you u
379379

380380
<%= link_to 'Back', products_path %>
381381

382-
Here we are rendering the same `_form` partial we used in the new view. Tu use this properly we need a `@product` instance variable populated via the controller. Unlike the `new` action, we want to edit a specific product. So we will have to look up that product from the database. Open up `app/views/controllers/products_controller.rb` and add an edit action:
382+
Here we are rendering the same `_form` partial we used in the new view. To use this properly we need a `@product` instance variable populated via the controller. Unlike the `new` action, we want to edit a specific product. So we will have to look up that product from the database. Open up `app/views/controllers/products_controller.rb` and add an edit action:
383383

384384

385385
def edit
@@ -437,7 +437,7 @@ this is a tool to pass on one time messages to the users of your application. To
437437
<p class="flash-notice"><%= flash[:notice] %></p>
438438
<% end %>
439439

440-
Inside of our controller we can have other types of flash messages by using the `flash` object. For instance you might set `flash[:error] = 'Please try again'` in the controller but you would still need to add the view logic to your layout. Flash messages are nice for a lightweight application but shouldn't be relied on too much. Also note that this type of inline :notice can only be used with redirects for now.
440+
Inside of our controller we can have other types of flash messages by using the `flash` object. For instance, you might set `flash[:error] = 'Please try again'` in the controller but you would still need to add the view logic to your layout. Flash messages are nice for a lightweight application but shouldn't be relied on too much. Also note that this type of inline :notice can only be used with redirects for now.
441441

442442

443443
Go back to [http://localhost:3000/products/1/edit](http://localhost:3000/products/1/edit) change the price and then hit enter. You should end up on the Products#show page with the extra flash message of "Product was successfully updated.".
@@ -464,7 +464,7 @@ Like we saw with the `redirect_to @product`, `link_to` understands that if you a
464464

465465
In addition to specifying the method, we're also telling our link_to to show the user a confirmation popup to ensure that they didn't click the destroy button by accident.
466466

467-
Go ahead and navigate to a product page like [http://localhost:3000/products/23](http://localhost:3000/products/23) and click the destroy link. You should be redirected to the products index page. To verify that product no longer exists you can go to the same url [http://localhost:3000/products/23](http://localhost:3000/products/23) and you should get an error since it no longer exists.
467+
Go ahead and navigate to a product page like [http://localhost:3000/products/23](http://localhost:3000/products/23) and click the destroy link. You should be redirected to the products index page. To verify that product no longer exists, you can go to the same url [http://localhost:3000/products/23](http://localhost:3000/products/23) and you should get an error since it no longer exists.
468468

469469

470470
Save and commit the results to GIT.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
11
class ProductsController < ApplicationController
2+
3+
def index
4+
@products = Product.includes(:user).all
5+
6+
end
7+
8+
def create
9+
@product = Product.new(params[:product])
10+
11+
respond_to do |format|
12+
if @product.save
13+
format.html { render :action => "create" }
14+
format.json { render :json => @product }
15+
else
16+
format.html { render :action => "new" }
17+
format.json { render :json => @product.errors, :status => :unprocessable_entity }
18+
end
19+
end
20+
end
21+
22+
def new
23+
@product = Product.new
24+
end
25+
26+
def show
27+
@product = Product.where(:id => params[:id]).first
28+
end
29+
30+
def edit
31+
@product = Product.where(:id => params[:id]).first
32+
end
33+
34+
def update
35+
@product = Product.where(:id => params[:id]).first
36+
37+
respond_to do |format|
38+
if @product.update_attributes(params[:product])
39+
format.html { redirect_to @product, :notice => 'Product was successfully updated.' }
40+
format.json { head :no_content }
41+
else
42+
format.html { render action: "edit" }
43+
format.json { render json: @product.errors, :status => :unprocessable_entity }
44+
end
45+
end
46+
end
47+
48+
def destroy
49+
@product = Product.where(:id => params[:id]).first
50+
@product.destroy
51+
52+
respond_to do |format|
53+
format.html { redirect_to products_url }
54+
format.json { head :no_content }
55+
end
56+
end
57+
258
end

app/views/layouts/application.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<%= link_to "a list of users", users_path %> |
1212
<%= link_to "a list of products", products_path %>
1313

14+
<% if flash[:notice].present? %>
15+
<p class="flash-notice"><%= flash[:notice] %></p>
16+
<% end %>
17+
1418
<%= yield %>
1519

1620
</body>

app/views/products/_form.html.erb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
<% if @product.errors.any? %>
4+
<div id="error_explanation">
5+
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this user from being saved:</h2>
6+
7+
<ul>
8+
<% @product.errors.full_messages.each do |msg| %>
9+
<li><%= msg %></li>
10+
<% end %>
11+
</ul>
12+
</div>
13+
<% end %>
14+
15+
16+
<%= form_for(@product) do |f| %>
17+
<div class="field">
18+
<%= f.label :name %><br />
19+
<%= f.text_field :name %>
20+
</div>
21+
<div class="field">
22+
<%= f.label :price %><br />
23+
<%= f.text_field :price %>
24+
</div>
25+
<div class="actions">
26+
<%= f.submit %>
27+
</div>
28+
<% end %>

app/views/products/create.html.erb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
<h2>Create View</h2>
22

3-
<%= params[:product].inspect %>
3+
<h2>Product Created Successfully<h2>
44

5-
6-
7-
<% product = Product.new(params[:product]) %>
8-
<%= product.save %>
9-
<%= product.inspect %>
10-
11-
<br />
12-
<% product.errors.inspect %>
13-
14-
<% if product.save %>
15-
<h2> Congrats You Created a New Product</h2>
16-
Your product looks like <%= product.inspect %>
17-
<% else %>
18-
<h2>Your product was not saved!! </h2>
19-
<%= product.errors.full_messages %>
20-
Please go back in your browser and fix the problem
21-
<% end %>
5+
<%= @product.name %> added to the website, it costs: $<%= @product.price %>

app/views/products/edit.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h2>Let's Edit a Product!</h2>
2+
3+
<%= render 'form' %>
4+
5+
<%= link_to 'Back', products_path %>

app/views/products/index.html.erb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
<h2> Products in the market </h2>
2-
<% lots_of_products = Product.includes(:user).all %>
3-
4-
<ul>
5-
<% lots_of_products.each do |product| %>
6-
<li>
7-
Product Name: "<%= product.name %>" costs $<%= product.price %> |
8-
Sold by <%= product.user.name if product.user.present? %>
9-
</li>
10-
<% end %>
11-
</ul>
2+
3+
<table>
4+
<tr>
5+
<th>ID</th>
6+
<th>Item</th>
7+
<th>Cost</th>
8+
<th>Seller</th>
9+
</tr>
10+
11+
<% @products.each do |product| %>
12+
<tr>
13+
<td><%= product.id %> &nbsp; </td>
14+
<td><%= product.name %> &nbsp; </td>
15+
<td>$<%= product.price %> &nbsp; </td>
16+
<td><%= product.user.name if product.user.present? %> &nbsp; </td>
17+
</tr>
18+
<% end %>
19+
20+
</table>
21+
22+
23+
24+
25+
26+
27+

app/views/products/new.html.erb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
11
<h2>Let's add new products!</h2>
2-
3-
<form method='post' action='/products' >
4-
<label>Name of your product</label><br>
5-
<input name="product[name]" size="30" type="text">
6-
7-
<br />
8-
<label>Price</label><br>
9-
<input name="product[price]" type="number">
10-
11-
<br />
12-
<input type="submit" value="Create Product">
13-
</form>
14-
2+
<%= render :partial => 'products/form' %>

app/views/products/show.html.erb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<p>
2+
<b>ID #:</b>
3+
<%= @product.id %>
4+
</p>
5+
6+
<p>
7+
<b>Name:</b>
8+
<%= @product.name %>
9+
</p>
10+
11+
<p>
12+
<b>Price:</b>
13+
<%= @product.price %>
14+
</p>
15+
16+
<%= link_to 'Destroy', @product, :method => :delete, :data => { :confirm => 'Are you sure?' } %>
17+
|
18+
19+
<%= link_to 'Edit', edit_product_path(@product), :method => :get %>

config/routes.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
ControllerExercise::Application.routes.draw do
22

33

4-
get '/products' => 'products#index'
5-
get '/products/new' => 'products#new'
6-
post '/products' => 'products#create'
4+
resources :products
75

86
resources :users
97

0 commit comments

Comments
 (0)