Skip to content

Commit 3f7c6ad

Browse files
committed
- server.js: fix cookies and redirects + more controller lessons
1 parent 87e1c32 commit 3f7c6ad

File tree

23 files changed

+444
-12
lines changed

23 files changed

+444
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/products-controller"
3+
}

src/content/tutorial/8-controllers/4-crud-show/_files/workspace/.keep

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ProductsController < ApplicationController
2+
def index
3+
@products = Product.all
4+
end
5+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>Products</h1>
2+
3+
<div id="products">
4+
<% @products.each do |product| %>
5+
<div>
6+
<%= product.name %>
7+
</div>
8+
<% end %>
9+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Rails.application.routes.draw do
2+
resources :products
3+
4+
# Defines the root path route ("/")
5+
root "products#index"
6+
end
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
type: lesson
3+
title: "CRUD: Showing Individual Products"
4+
focus: /workspace/store/app/controllers/products_controller.rb
5+
previews:
6+
- 3000
7+
custom:
8+
shell:
9+
workdir: "/workspace/store"
10+
---
11+
12+
### CRUD Actions
13+
14+
We need to be able to access individual products. This is the R in CRUD to read
15+
a resource.
16+
17+
We've already defined the route for individual products with our
18+
`resources :products` route. This generates `/products/:id` as a route that
19+
points to `products#show`.
20+
21+
Now we need to add that action to the `ProductsController` and define what
22+
happens when it is called.
23+
24+
Open the Products controller and add the `show` action like this:
25+
26+
```ruby ins={5-8}
27+
class ProductsController < ApplicationController
28+
def index
29+
@products = Product.all
30+
end
31+
32+
def show
33+
@product = Product.find(params[:id])
34+
end
35+
end
36+
```
37+
38+
The `show` action here defines the *singular* `@product` because it's loading a
39+
single record from the database, in other words: Show this one product. We use
40+
plural `@products` in `index` because we're loading multiple products.
41+
42+
To query the database, we use `params` to access the request parameters. In this
43+
case, we're using the `:id` from our route `/products/:id`. When we visit
44+
`/products/1`, the params hash contains `{id: 1}` which results in our `show`
45+
action calling `Product.find(1)` to load Product with ID of `1` from the
46+
database.
47+
48+
We need a view for the show action next. Following the Rails naming conventions,
49+
the `ProductsController` expects views in `app/views` in a subfolder named
50+
`products`.
51+
52+
The `show` action expects a file in `app/views/products/show.html.erb`. Let's
53+
create that file in our editor and add the following contents:
54+
55+
```erb
56+
<h1><%= @product.name %></h1>
57+
58+
<%= link_to "Back", products_path %>
59+
```
60+
61+
It would be helpful for the index page to link to the show page for each product
62+
so we can click on them to navigate. We can update the
63+
`app/views/products/index.html.erb` view to link to this new page to use an
64+
anchor tag to the path for the `show` action.
65+
66+
```erb {6-8}
67+
<h1>Products</h1>
68+
69+
<div id="products">
70+
<% @products.each do |product| %>
71+
<div>
72+
<a href="/products/<%= product.id %>">
73+
<%= product.name %>
74+
</a>
75+
</div>
76+
<% end %>
77+
</div>
78+
```
79+
80+
Refresh this page in your browser and you'll see that this works, but we can do
81+
better.
82+
83+
Rails provides helper methods for generating paths and URLs. When you run
84+
`bin/rails routes`, you'll see the Prefix column. This prefix matches the
85+
helpers you can use for generating URLs with Ruby code.
86+
87+
```
88+
Prefix Verb URI Pattern Controller#Action
89+
products GET /products(.:format) products#index
90+
product GET /products/:id(.:format) products#show
91+
```
92+
93+
These route prefixes give us helpers like the following:
94+
95+
* `products_path` generates `"/products"`
96+
* `products_url` generates `"http://localhost:3000/products"`
97+
* `product_path(1)` generates `"/products/1"`
98+
* `product_url(1)` generates `"http://localhost:3000/products/1"`
99+
100+
`_path` returns a relative path which the browser understands is for the current
101+
domain.
102+
103+
`_url` returns a full URL including the protocol, host, and port.
104+
105+
URL helpers are useful for rendering emails that will be viewed outside of the
106+
browser.
107+
108+
Combined with the `link_to` helper, we can generate anchor tags and use the URL
109+
helper to do this cleanly in Ruby. `link_to` accepts the display content for the
110+
link (`product.name`) and the path or URL to link to for the `href` attribute
111+
(`product`).
112+
113+
Let's refactor this to use these helpers:
114+
115+
```erb {6}
116+
<h1>Products</h1>
117+
118+
<div id="products">
119+
<% @products.each do |product| %>
120+
<div>
121+
<%= link_to product.name, product_path(product.id) %>
122+
</div>
123+
<% end %>
124+
</div>
125+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/products-controller"
3+
}

src/content/tutorial/8-controllers/5-crud-create/_files/workspace/.keep

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ProductsController < ApplicationController
2+
def index
3+
@products = Product.all
4+
end
5+
6+
def show
7+
@product = Product.find(params[:id])
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>Products</h1>
2+
3+
<div id="products">
4+
<% @products.each do |product| %>
5+
<div>
6+
<%= link_to product.name, product_path(product.id) %>
7+
</div>
8+
<% end %>
9+
</div>

0 commit comments

Comments
 (0)