Skip to content

Commit 676d424

Browse files
committed
+ part: authentication
1 parent 9ade497 commit 676d424

File tree

53 files changed

+723
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+723
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/authentication"
3+
}
File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class ProductsController < ApplicationController
2+
before_action :set_product, only: %i[ show edit update destroy]
3+
4+
def index
5+
@products = Product.all
6+
end
7+
8+
def show
9+
end
10+
11+
def new
12+
@product = Product.new
13+
end
14+
15+
def create
16+
@product = Product.new(product_params)
17+
if @product.save
18+
redirect_to @product
19+
else
20+
render :new, status: :unprocessable_entity
21+
end
22+
end
23+
24+
def edit
25+
end
26+
27+
def update
28+
if @product.update(product_params)
29+
redirect_to @product
30+
else
31+
render :edit, status: :unprocessable_entity
32+
end
33+
end
34+
35+
def destroy
36+
@product.destroy
37+
redirect_to products_path
38+
end
39+
40+
private
41+
def set_product
42+
@product = Product.find(params[:id])
43+
end
44+
45+
def product_params
46+
params.expect(product: [ :name ])
47+
end
48+
end
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h1>Products</h1>
2+
3+
<%= link_to "New product", new_product_path %>
4+
5+
<div id="products">
6+
<% @products.each do |product| %>
7+
<div>
8+
<%= link_to product.name, product_path(product.id) %>
9+
</div>
10+
<% end %>
11+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1><%= @product.name %></h1>
2+
3+
<%= link_to "Back", products_path %>
4+
<%= link_to "Edit", edit_product_path(@product) %>
5+
<%= button_to "Delete", @product, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
type: lesson
3+
title: Adding Log Out
4+
focus: /workspace/store/app/views/layouts/application.html.erb
5+
previews:
6+
- 3000
7+
custom:
8+
shell:
9+
workdir: "/workspace/store"
10+
---
11+
12+
### Adding Log Out
13+
14+
To log out of the application, we can add a button to the top of
15+
`app/views/layouts/application.html.erb`. This layout is where you put HTML that
16+
you want to include in every page like a header or footer.
17+
18+
Add a small `<nav>` section inside the `<body>` with a link to Home and a Log
19+
out button and wrap `yield` with a `<main>` tag.
20+
21+
```erb ins={5-8,10,12}
22+
<!DOCTYPE html>
23+
<html>
24+
<!-- ... -->
25+
<body>
26+
<nav>
27+
<%= link_to "Home", root_path %>
28+
<%= button_to "Log out", session_path, method: :delete if authenticated? %>
29+
</nav>
30+
31+
<main>
32+
<%= yield %>
33+
</main>
34+
</body>
35+
</html>
36+
```
37+
38+
This will display a Log out button only if the user is authenticated. When
39+
clicked, it will send a DELETE request to the session path which will log the
40+
user out.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/authentication"
3+
}
File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class ProductsController < ApplicationController
2+
before_action :set_product, only: %i[ show edit update destroy]
3+
4+
def index
5+
@products = Product.all
6+
end
7+
8+
def show
9+
end
10+
11+
def new
12+
@product = Product.new
13+
end
14+
15+
def create
16+
@product = Product.new(product_params)
17+
if @product.save
18+
redirect_to @product
19+
else
20+
render :new, status: :unprocessable_entity
21+
end
22+
end
23+
24+
def edit
25+
end
26+
27+
def update
28+
if @product.update(product_params)
29+
redirect_to @product
30+
else
31+
render :edit, status: :unprocessable_entity
32+
end
33+
end
34+
35+
def destroy
36+
@product.destroy
37+
redirect_to products_path
38+
end
39+
40+
private
41+
def set_product
42+
@product = Product.find(params[:id])
43+
end
44+
45+
def product_params
46+
params.expect(product: [ :name ])
47+
end
48+
end

0 commit comments

Comments
 (0)