Skip to content

Commit 45c28c4

Browse files
committed
+ action_text + active_storage
1 parent d356d15 commit 45c28c4

File tree

39 files changed

+1727
-32
lines changed

39 files changed

+1727
-32
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/active-storage"
3+
}

src/content/tutorial/11-rich-text/1-action-text/_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+
<% cache @product do %>
2+
<h1><%= @product.name %></h1>
3+
<% end %>
4+
5+
<%= link_to "Back", products_path %>
6+
<% if authenticated? %>
7+
<%= link_to "Edit", edit_product_path(@product) %>
8+
<%= button_to "Delete", @product, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
9+
<% end %>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
type: lesson
3+
title: Rich Text Fields with Action Text
4+
focus: /workspace/store/app/models/product.rb
5+
previews:
6+
- 3000
7+
custom:
8+
shell:
9+
workdir: "/workspace/store"
10+
---
11+
12+
Rich Text Fields with Action Text
13+
---------------------------------
14+
15+
Many applications need rich text with embeds (i.e. multimedia elements) and
16+
Rails provides this functionality out of the box with Action Text.
17+
18+
To use Action Text, you'll first run the installer:
19+
20+
```bash
21+
$ bin/rails action_text:install
22+
$ bin/rails db:migrate
23+
```
24+
25+
Restart your Rails server to make sure all the new features are loaded.
26+
27+
Now, let's add a rich text description field to our product.
28+
29+
First, add the following to the `Product` model:
30+
31+
```ruby ins={2}
32+
class Product < ApplicationRecord
33+
has_rich_text :description
34+
validates :name, presence: true
35+
end
36+
```
37+
38+
The form can now be updated to include a rich text field for editing the
39+
description in `app/views/products/_form.html.erb` before the submit button.
40+
41+
```erb ins={4-7}
42+
<%= form_with model: product do |form| %>
43+
<%# ... %>
44+
45+
<div>
46+
<%= form.label :description, style: "display: block" %>
47+
<%= form.rich_textarea :description %>
48+
</div>
49+
50+
<div>
51+
<%= form.submit %>
52+
</div>
53+
<% end %>
54+
```
55+
56+
Our controller also needs to permit this new parameter when the form is
57+
submitted, so we'll update the permitted params to include description in
58+
`app/controllers/products_controller.rb`
59+
60+
```ruby ins={3}
61+
# Only allow a list of trusted parameters through.
62+
def product_params
63+
params.expect(product: [ :name, :description ])
64+
end
65+
```
66+
67+
We also need to update the show view to display the description in
68+
`app/views/products/show.html.erb`:
69+
70+
```erb ins={3}
71+
<% cache @product do %>
72+
<h1><%= @product.name %></h1>
73+
<%= @product.description %>
74+
<% end %>
75+
```
76+
77+
The cache key generated by Rails also changes when the view is modified. This
78+
makes sure the cache stays in sync with the latest version of the view template.
79+
80+
Create a new product and add a description with bold and italic text. You'll see
81+
that the show page displays the formatted text and editing the product retains
82+
this rich text in the text area.
83+
84+
Check out the [Action Text Overview](https://guides.rubyonrails.org/action_text_overview.html) to learn more.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
type: part
3+
title: Rich Text
4+
prepareCommands:
5+
- ['npm install', 'Preparing Ruby runtime']
6+
- ['node scripts/rails.js db:prepare', 'Prepare development database']
7+
---
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/active-storage"
3+
}

src/content/tutorial/12-file-uploads/1-active-storage/_files/workspace/.keep

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class ProductsController < ApplicationController
2+
allow_unauthenticated_access only: %i[ index show ]
3+
before_action :set_product, only: %i[ show edit update destroy]
4+
5+
def index
6+
@products = Product.all
7+
end
8+
9+
def show
10+
end
11+
12+
def new
13+
@product = Product.new
14+
end
15+
16+
def create
17+
@product = Product.new(product_params)
18+
if @product.save
19+
redirect_to @product
20+
else
21+
render :new, status: :unprocessable_entity
22+
end
23+
end
24+
25+
def edit
26+
end
27+
28+
def update
29+
if @product.update(product_params)
30+
redirect_to @product
31+
else
32+
render :edit, status: :unprocessable_entity
33+
end
34+
end
35+
36+
def destroy
37+
@product.destroy
38+
redirect_to products_path
39+
end
40+
41+
private
42+
def set_product
43+
@product = Product.find(params[:id])
44+
end
45+
46+
def product_params
47+
params.expect(product: [ :name, :description ])
48+
end
49+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Product < ApplicationRecord
2+
has_rich_text :description
3+
validates :name, presence: true
4+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%= form_with model: product do |form| %>
2+
<div>
3+
<%= form.label :name %>
4+
<%= form.text_field :name %>
5+
</div>
6+
7+
<div>
8+
<%= form.label :description, style: "display: block" %>
9+
<%= form.rich_textarea :description %>
10+
</div>
11+
12+
<div>
13+
<%= form.submit %>
14+
</div>
15+
<% end %>

0 commit comments

Comments
 (0)