Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Your numbers might be different but it should be more than 1.

# 2) Modify Views and Logging

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.
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.

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

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


If you don't go back and follow the prior steps.
If you don't, go back and follow the prior steps.

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

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

(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.)

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:
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:

Processing by ProductsController#index as HTML

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.
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.

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

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.
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.

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

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

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.
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.

#### Homework:

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:
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:


HTTP verb used in this request:
URL:
Controller Name:
Controller Action:
View File Name:
Layout File Name:
Response code of the request:
HTTP verb used in this request: GET
URL: "/users"
Controller Name: UsersController
Controller Action: #index
View File Name: #index
Layout File Name: users/index.html.erb
Response code of the request: 200

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) ?
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) ? SELECT "users".* FROM "users"

Why do you think this line is there?
Why do you think this line is there? select all users from users model



Expand Down
1 change: 1 addition & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Product < ActiveRecord::Base
belongs_to :user
attr_accessible :name, :price
validates :name, :uniqueness => true
end
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<%= csrf_meta_tags %>
</head>
<body>
<%= link_to "Schneems Blog", "http://schneems.com" %>
<%= link_to "User List", users_path %>
<%= link_to "Product List", products_path %>

<%= yield %>

Expand Down
16 changes: 16 additions & 0 deletions app/views/products/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Create View</h2>

<%= params[:product].inspect %>

<% product = Product.new(params[:product]) %>
<%= product.save %>
<%= product.inspect %>
<br />
<% if product.save %>
<h2> Congrats You Created a New Product</h2>
Your product looks like <%= product.inspect %>
<% else %>
<h2>Your product was not saved!! </h2>
<%= product.errors.full_messages %>
Please go back in your browser and fix the problem
<% end %>
14 changes: 10 additions & 4 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<h2> I AMA View </h2>
<p>
Find me in <%= Rails.root.join("app", "views", "products", __FILE__ ) %>
</p>
<% lots_of_products = Product.includes(:user).all %>

<ul>
<% lots_of_products.each do |product| %>
<li>
Product Name: "<%= product.name %>"" costs $<%= product.price %>
Sold by <%= product.user.name %>
</li>
<% end %>
</ul>
15 changes: 15 additions & 0 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2>New Products View</h2>

<form method='post' action='/products' >
<label>Name</label><br>
<input name="product[name]" size="30" type="text">

<br />
<label>Price</label><br>
<input name="product[price]" type="number">


<input type="submit" value="Create Product">


</form>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


get '/products' => 'products#index'
get '/products/new' => 'products#new'
post '/products' => 'products#create'

resources :users

Expand Down