Skip to content

Commit af3f7ff

Browse files
committed
Update Project
1 parent 4010df5 commit af3f7ff

7 files changed

Lines changed: 69 additions & 18 deletions

File tree

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Your numbers might be different but it should be more than 1.
153153

154154
# 2) Modify Views and Logging
155155

156-
Now that we have a bunch of users and associated products lets do something useful with them. Previously we noted that [localhost:3000/products](http://localhost:3000/products) was linked to the view `app/views/products/index.html.erb` through a route in our `config/routes.rb` file. Open the `index.html.erb` view now in a text editor (i recommend sublime text 2 for mac). Make sure your rails server is started (`$ rails server`) and visit [localhost:3000/products](http://localhost:3000/products) in your browser.
156+
Now that we have a bunch of users and associated products, lets do something useful with them. Previously we noted that [localhost:3000/products](http://localhost:3000/products) was linked to the view `app/views/products/index.html.erb` through a route in our `config/routes.rb` file. Open the `index.html.erb` view now in a text editor (i recommend sublime text 2 for mac). Make sure your rails server is started (`$ rails server`) and visit [localhost:3000/products](http://localhost:3000/products) in your browser.
157157

158158
Add this to the top of the `index.html.erb` file:
159159

@@ -162,7 +162,7 @@ Add this to the top of the `index.html.erb` file:
162162
When you refresh your page you should see the new text:
163163

164164

165-
If you don't go back and follow the prior steps.
165+
If you don't, go back and follow the prior steps.
166166

167167
If you look in your rails server log (this is the code that gets spewed from terminal after you run `$ rails server`), you should be able to see a line in there that looks like this
168168

@@ -171,15 +171,15 @@ If you look in your rails server log (this is the code that gets spewed from ter
171171

172172
(Note: we are using the `quiet_assets` gem, if you do this on another project your output will still have the same info, but it will also have a bunch of useless output for debugging as well.)
173173

174-
This is telling us that we are using a GET request on the url `/products` url, and since our routes have that mapped to `Products#index` in our routes.rb file our server log will tell us that combination of HTTP action and URL that we are looking at is located in the products controller and index action:
174+
This is telling us that we are using a GET request on the `/products` url, and since our routes have that mapped to `products#index` in our routes.rb file, our server log will tell us that combination of HTTP action and URL that we are looking at is located in the products controller and index action:
175175

176176
Processing by ProductsController#index as HTML
177177

178-
Finally it will tell us that the view it rendered is coming from `products/index.html.erb` and that is is using the `laouts/application` file.
178+
Finally it will tell us that the view it rendered is coming from `products/index.html.erb` and that is is using the `layouts/application` file.
179179

180180
Rendered products/index.html.erb within layouts/application (0.2ms)
181181

182-
We also learn that the request was a 200 response which is how computers say everything was good. If it was not a good response we might see a `404` or `500` reponse. On redirects we can expect a `301` or `302` response.
182+
We also learn that the request was a 200 response which is how computers say everything was good. If it was not a good response we might see a `404` or `500` response. On redirects we can expect a `301` or `302` response.
183183

184184
Completed 200 OK in 7ms (Views: 6.7ms | ActiveRecord: 0.0ms)
185185

@@ -190,25 +190,28 @@ All together the log looks like this:
190190
Rendered products/index.html.erb within layouts/application (0.2ms)
191191
Completed 200 OK in 7ms (Views: 6.7ms | ActiveRecord: 0.0ms)
192192

193-
There is alot of information in a tiny package. When things go wrong in your app you can use the log output to verify your assumptions are correct, and to get error messages.
193+
There is a lot of information in a tiny package. When things go wrong in your app, you can use the log output to verify your assumptions are correct and to get error messages.
194194

195195
#### Homework:
196196

197-
Visit a [localhost:3000/users](http://localhost:3000/users) and then find the log entry. Then open up the readme.md you coppied onto your local machine and fill out this information:
197+
Visit this url: [localhost:3000/users](http://localhost:3000/users) and then find the log entry. Then open up the readme.md you copied onto your local machine and fill out this information:
198198

199199

200-
HTTP verb used in this request:
201-
URL:
202-
Controller Name:
203-
Controller Action:
204-
View File Name:
205-
Layout File Name:
206-
Response code of the request:
200+
HTTP verb used in this request: Get
201+
URL: /users
202+
Controller Name: UsersController
203+
Controller Action: index
204+
View File Name: users/index.html.erb
205+
Layout File Name: layouts/application
206+
Response code of the request: 200
207207

208208
You should also notice a new line or two that we didn't see before, what is it (copy and paste, hint: after User Load) ?
209209

210+
User Load (0.5ms) SELECT "users".* FROM "users"
211+
210212
Why do you think this line is there?
211213

214+
It's the sql code to query the database
212215

213216

214217
Save and commit your answers.

app/models/product.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Product < ActiveRecord::Base
22
belongs_to :user
33
attr_accessible :name, :price
4+
validates :name, :uniqueness => true
45
end

app/views/layouts/application.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
</head>
99
<body>
1010

11+
<%= link_to "a list of users", users_path %> |
12+
<%= link_to "a list of products", products_path %>
13+
1114
<%= yield %>
1215

1316
</body>

app/views/products/create.html.erb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h2>Create View</h2>
2+
3+
<%= params[:product].inspect %>
4+
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 %>

app/views/products/index.html.erb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
<h2> I AMA View </h2>
2-
<p>
3-
Find me in <%= Rails.root.join("app", "views", "products", __FILE__ ) %>
4-
</p>
1+
<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>

app/views/products/new.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<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+

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
get '/products' => 'products#index'
5+
get '/products/new' => 'products#new'
6+
post '/products' => 'products#create'
57

68
resources :users
79

0 commit comments

Comments
 (0)