Skip to content

Commit c2770c7

Browse files
committed
Add more code highlights and make a few things more consistent
1 parent 6c5e5f7 commit c2770c7

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

guides/source/getting_started.md

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ The `show` action expects a file in `app/views/products/show.html.erb`. Let's cr
844844
<%= link_to "Back", products_path %>
845845
```
846846

847-
It would be helpful for the index page to link to the show page for each product so we can click on them to navigate. We can update the `index.html.erb` view to link to this new page to use an anchor tag to the path for the `show` action.
847+
It would be helpful for the index page to link to the show page for each product so we can click on them to navigate. We can update the `app/views/products/index.html.erb` view to link to this new page to use an anchor tag to the path for the `show` action.
848848

849849
```erb#6,8
850850
<h1>Products</h1>
@@ -985,7 +985,7 @@ Because we passed a new `Product` instance to the form builder, it automatically
985985

986986
To handle this, we first need to implement the `create` action in our controller.
987987

988-
```ruby#14-27
988+
```ruby#14-26
989989
class ProductsController < ApplicationController
990990
def index
991991
@products = Product.all
@@ -1103,7 +1103,7 @@ A `before_action` allows you to extract shared code between actions and run it *
11031103

11041104
This is a good example of the DRY (Don't Repeat Yourself) philosophy in action.
11051105

1106-
```ruby#2,8-9,24-25,27-33,37-39
1106+
```ruby#2,8-9,24-25,27-33,36-38
11071107
class ProductsController < ApplicationController
11081108
before_action :set_product, only: %i[ show edit update ]
11091109
@@ -1170,7 +1170,7 @@ We also want to replace any instance variables with a local variable, which we c
11701170
<% end %>
11711171
```
11721172

1173-
To use this partial in our new view, we can replace the form with a render call:
1173+
To use this partial in our `app/views/products/new.html.erb` view, we can replace the form with a render call:
11741174

11751175
```erb#3
11761176
<h1>New product</h1>
@@ -1249,7 +1249,7 @@ end
12491249

12501250
To make this work, we need to add a Delete button to `app/views/products/show.html.erb`:
12511251

1252-
```erb
1252+
```erb#5
12531253
<h1><%= @product.name %></h1>
12541254
12551255
<%= link_to "Back", products_path %>
@@ -1311,7 +1311,7 @@ To log out of the application, we can add a button to the top of `app/views/layo
13111311

13121312
Add a small `<nav>` section inside the `<body>` with a link to Home and a Log out button.
13131313

1314-
```erb
1314+
```erb#5-8
13151315
<!DOCTYPE html>
13161316
<html>
13171317
<!-- ... -->
@@ -1334,7 +1334,7 @@ However, our store's product index and show pages should be accessible to everyo
13341334

13351335
To allow guests to view products, we can allow unauthenticated access in our controller.
13361336

1337-
```ruby
1337+
```ruby#2
13381338
class ProductsController < ApplicationController
13391339
allow_unauthenticated_access only: %i[ index show ]
13401340
# ...
@@ -1345,7 +1345,7 @@ Log out and visit the products index and show pages to see they're accessible wi
13451345

13461346
### Showing Links for Authenticated Users Only
13471347

1348-
Since only logged in users can create products, we can modify the index view to only display the new product link if the user is authenticated.
1348+
Since only logged in users can create products, we can modify the `app/views/products/index.html.erb` view to only display the new product link if the user is authenticated.
13491349

13501350
```erb
13511351
<%= link_to "New product", new_product_path if authenticated? %>
@@ -1359,9 +1359,12 @@ Optionally, you can include a link to this route in the navbar to add a Login li
13591359
<%= link_to "Login", new_session_path unless authenticated? %>
13601360
```
13611361

1362-
You can also update the Edit and Destroy links on the show view to only display if authenticated.
1362+
You can also update the Edit and Destroy links on the `app/views/products/show.html.erb` view to only display if authenticated.
13631363

1364-
```erb#1,4
1364+
```erb#4,7
1365+
<h1><%= @product.name %></h1>
1366+
1367+
<%= link_to "Back", products_path %>
13651368
<% if authenticated? %>
13661369
<%= link_to "Edit", edit_product_path(@product) %>
13671370
<%= button_to "Destroy", @product, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
@@ -1383,7 +1386,11 @@ Using the `cache` method, we can store HTML in the cache. Let's cache the header
13831386

13841387
By passing `@product` into `cache`, Rails generates a unique cache key for the product. Active Record objects have a `cache_key` method that returns a String like `"products/1"`. The `cache` helper in the views combines this with the template digest to create a unique key for this HTML.
13851388

1386-
To enable caching in development, run `bin/rails dev:cache` in your terminal.
1389+
To enable caching in development, run the following command in your terminal.
1390+
1391+
```bash
1392+
$ bin/rails dev:cache
1393+
```
13871394

13881395
When you visit a product's show action (like `/products/2`), you'll see the new caching lines in your Rails server logs:
13891396

@@ -1623,7 +1630,7 @@ es:
16231630

16241631
We need to tell Rails which locale to use. The simplest option is to look for a locale param in the URL. We can do this in `app/controllers/application_controller.rb` with the following:
16251632

1626-
```ruby
1633+
```ruby#4,6-9
16271634
class ApplicationController < ActionController::Base
16281635
# ...
16291636
@@ -1695,7 +1702,7 @@ $ bin/rails db:migrate
16951702

16961703
We'll need to add the inventory count to the product form in `app/views/products/_form.html.erb`.
16971704

1698-
```erb
1705+
```erb#4-7
16991706
<%= form_with model: product do |form| %>
17001707
<%# ... %>
17011708
@@ -1712,15 +1719,15 @@ We'll need to add the inventory count to the product form in `app/views/products
17121719

17131720
The controller also needs `:inventory_count` added to the permitted parameters.
17141721

1715-
```ruby
1722+
```ruby#2
17161723
def product_params
17171724
params.expect(product: [ :name, :description, :featured_image, :inventory_count ])
17181725
end
17191726
```
17201727

17211728
It would also be helpful to validate that our inventory count is never a negative number, so let's also add a validation for that in our model.
17221729

1723-
```ruby
1730+
```ruby#6
17241731
class Product < ApplicationRecord
17251732
has_one_attached :featured_image
17261733
has_rich_text :description
@@ -1791,7 +1798,7 @@ Our redirect sets a notice in the Rails flash. The flash is used for storing mes
17911798

17921799
To display the flash message, let's add the notice to `app/views/layouts/application.html.erb` inside the body:
17931800

1794-
```erb
1801+
```erb#4
17951802
<html>
17961803
<!-- ... -->
17971804
<body>
@@ -1847,7 +1854,7 @@ This generates a class at `app/mailers/product_mailer.rb` with an `in_stock` met
18471854

18481855
Update this method to mail to a subscriber's email address.
18491856

1850-
```ruby
1857+
```ruby#7-10
18511858
class ProductMailer < ApplicationMailer
18521859
# Subject can be set in your I18n file at config/locales/en.yml
18531860
# with the following lookup:
@@ -2013,6 +2020,7 @@ Now that the code triggering the notification has been extracted into the Notifi
20132020
class Product < ApplicationRecord
20142021
include Notifications
20152022
2023+
has_many :subscribers, dependent: :destroy
20162024
has_one_attached :featured_image
20172025
has_rich_text :description
20182026
@@ -2037,14 +2045,14 @@ First, we need a route for unsubscribing that will be the URL we include in emai
20372045

20382046
Active Record has a feature called `generates_token_for` that can generate unique tokens to find database records for different purposes. We can use this for generating a unique unsubscribe token to use in the email's unsubscribe URL.
20392047

2040-
```ruby
2048+
```ruby#3
20412049
class Subscriber < ApplicationRecord
20422050
belongs_to :product
20432051
generates_token_for :unsubscribe
20442052
end
20452053
```
20462054

2047-
Our controller will first look up the Subscriber record from the token in the URL. Once the subscriber is found, it will destroy the record and redirect to the homepage.
2055+
Our controller will first look up the Subscriber record from the token in the URL. Once the subscriber is found, it will destroy the record and redirect to the homepage. Create `app/controllers/unsubscribes_controller.rb` and add the following code:
20482056

20492057
```ruby
20502058
class UnsubscribesController < ApplicationController
@@ -2068,7 +2076,7 @@ Last but not least, let's add the unsubscribe link to our email templates.
20682076

20692077
In `app/views/product_mailer/in_stock.html.erb`, add a `link_to`:
20702078

2071-
```erb
2079+
```erb#5
20722080
<h1>Good news!</h1>
20732081
20742082
<p><%= link_to @product.name, product_url(@product) %> is back in stock.</p>
@@ -2078,7 +2086,7 @@ In `app/views/product_mailer/in_stock.html.erb`, add a `link_to`:
20782086

20792087
In `app/views/product_mailer/in_stock.text.erb`, add the URL in plain text:
20802088

2081-
```erb
2089+
```erb#6
20822090
Good news!
20832091
20842092
<%= @product.name %> is back in stock.
@@ -2104,19 +2112,17 @@ Fixtures are predefined sets of data that populate your test database before run
21042112

21052113
This file will be empty by default - you need to populate it with fixtures for your tests.
21062114

2107-
Let’s update the product fixtures file with this data:
2115+
Let’s update the product fixtures file at `test/fixtures/products.yml` with the following:
21082116

21092117
```yaml
2110-
# test/fixtures/products.yml
21112118
tshirt:
21122119
name: T-Shirt
21132120
inventory_count: 15
21142121
```
21152122
2116-
And for subscribers, let's add these two fixtures:
2123+
And for subscribers, let's add these two fixtures to `test/fixtures/subscribers.yml`:
21172124

21182125
```yaml
2119-
# test/fixtures/subscribers.yml
21202126
david:
21212127
product: tshirt
21222128
@@ -2126,7 +2132,7 @@ chris:
21262132
21272133
```
21282134

2129-
You'll notice that we can reference the Product fixture by name here. Rails associates this automatically for us in the database so we don't have to manage record IDs and associations in tests.
2135+
You'll notice that we can reference the `Product` fixture by name here. Rails associates this automatically for us in the database so we don't have to manage record IDs and associations in tests.
21302136

21312137
These fixtures will be automatically inserted into the database when we run our test suite.
21322138

@@ -2224,14 +2230,17 @@ We can check our code for consistency by running:
22242230

22252231
```bash
22262232
$ bin/rubocop
2233+
```
2234+
2235+
This will print out any offenses and tell you what they are.
2236+
2237+
```bash
22272238
Inspecting 53 files
22282239
.....................................................
22292240
22302241
53 files inspected, no offenses detected
22312242
```
22322243

2233-
This will print out any offenses and tell you what they are.
2234-
22352244
Rubocop can automatically fix offsenses using the `--autocorrect` flag (or its short version `-a`).
22362245

22372246
```

0 commit comments

Comments
 (0)