You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/getting_started.md
+37-28Lines changed: 37 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -844,7 +844,7 @@ The `show` action expects a file in `app/views/products/show.html.erb`. Let's cr
844
844
<%= link_to "Back", products_path %>
845
845
```
846
846
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.
848
848
849
849
```erb#6,8
850
850
<h1>Products</h1>
@@ -985,7 +985,7 @@ Because we passed a new `Product` instance to the form builder, it automatically
985
985
986
986
To handle this, we first need to implement the `create` action in our controller.
987
987
988
-
```ruby#14-27
988
+
```ruby#14-26
989
989
class ProductsController < ApplicationController
990
990
def index
991
991
@products = Product.all
@@ -1103,7 +1103,7 @@ A `before_action` allows you to extract shared code between actions and run it *
1103
1103
1104
1104
This is a good example of the DRY (Don't Repeat Yourself) philosophy in action.
1105
1105
1106
-
```ruby#2,8-9,24-25,27-33,37-39
1106
+
```ruby#2,8-9,24-25,27-33,36-38
1107
1107
class ProductsController < ApplicationController
1108
1108
before_action :set_product, only: %i[ show edit update ]
1109
1109
@@ -1170,7 +1170,7 @@ We also want to replace any instance variables with a local variable, which we c
1170
1170
<% end %>
1171
1171
```
1172
1172
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:
1174
1174
1175
1175
```erb#3
1176
1176
<h1>New product</h1>
@@ -1249,7 +1249,7 @@ end
1249
1249
1250
1250
To make this work, we need to add a Delete button to `app/views/products/show.html.erb`:
1251
1251
1252
-
```erb
1252
+
```erb#5
1253
1253
<h1><%= @product.name %></h1>
1254
1254
1255
1255
<%= 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
1311
1311
1312
1312
Add a small `<nav>` section inside the `<body>` with a link to Home and a Log out button.
1313
1313
1314
-
```erb
1314
+
```erb#5-8
1315
1315
<!DOCTYPE html>
1316
1316
<html>
1317
1317
<!-- ... -->
@@ -1334,7 +1334,7 @@ However, our store's product index and show pages should be accessible to everyo
1334
1334
1335
1335
To allow guests to view products, we can allow unauthenticated access in our controller.
1336
1336
1337
-
```ruby
1337
+
```ruby#2
1338
1338
class ProductsController < ApplicationController
1339
1339
allow_unauthenticated_access only: %i[ index show ]
1340
1340
# ...
@@ -1345,7 +1345,7 @@ Log out and visit the products index and show pages to see they're accessible wi
1345
1345
1346
1346
### Showing Links for Authenticated Users Only
1347
1347
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.
1349
1349
1350
1350
```erb
1351
1351
<%= 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
@@ -1383,7 +1386,11 @@ Using the `cache` method, we can store HTML in the cache. Let's cache the header
1383
1386
1384
1387
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.
1385
1388
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
+
```
1387
1394
1388
1395
When you visit a product's show action (like `/products/2`), you'll see the new caching lines in your Rails server logs:
1389
1396
@@ -1623,7 +1630,7 @@ es:
1623
1630
1624
1631
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:
1625
1632
1626
-
```ruby
1633
+
```ruby#4,6-9
1627
1634
class ApplicationController < ActionController::Base
1628
1635
# ...
1629
1636
@@ -1695,7 +1702,7 @@ $ bin/rails db:migrate
1695
1702
1696
1703
We'll need to add the inventory count to the product form in `app/views/products/_form.html.erb`.
1697
1704
1698
-
```erb
1705
+
```erb#4-7
1699
1706
<%= form_with model: product do |form| %>
1700
1707
<%# ... %>
1701
1708
@@ -1712,15 +1719,15 @@ We'll need to add the inventory count to the product form in `app/views/products
1712
1719
1713
1720
The controller also needs `:inventory_count` added to the permitted parameters.
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.
1722
1729
1723
-
```ruby
1730
+
```ruby#6
1724
1731
class Product < ApplicationRecord
1725
1732
has_one_attached :featured_image
1726
1733
has_rich_text :description
@@ -1791,7 +1798,7 @@ Our redirect sets a notice in the Rails flash. The flash is used for storing mes
1791
1798
1792
1799
To display the flash message, let's add the notice to `app/views/layouts/application.html.erb` inside the body:
1793
1800
1794
-
```erb
1801
+
```erb#4
1795
1802
<html>
1796
1803
<!-- ... -->
1797
1804
<body>
@@ -1847,7 +1854,7 @@ This generates a class at `app/mailers/product_mailer.rb` with an `in_stock` met
1847
1854
1848
1855
Update this method to mail to a subscriber's email address.
1849
1856
1850
-
```ruby
1857
+
```ruby#7-10
1851
1858
class ProductMailer < ApplicationMailer
1852
1859
# Subject can be set in your I18n file at config/locales/en.yml
1853
1860
# with the following lookup:
@@ -2013,6 +2020,7 @@ Now that the code triggering the notification has been extracted into the Notifi
2013
2020
class Product < ApplicationRecord
2014
2021
include Notifications
2015
2022
2023
+
has_many :subscribers, dependent: :destroy
2016
2024
has_one_attached :featured_image
2017
2025
has_rich_text :description
2018
2026
@@ -2037,14 +2045,14 @@ First, we need a route for unsubscribing that will be the URL we include in emai
2037
2045
2038
2046
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.
2039
2047
2040
-
```ruby
2048
+
```ruby#3
2041
2049
class Subscriber < ApplicationRecord
2042
2050
belongs_to :product
2043
2051
generates_token_for :unsubscribe
2044
2052
end
2045
2053
```
2046
2054
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:
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.
2130
2136
2131
2137
These fixtures will be automatically inserted into the database when we run our test suite.
2132
2138
@@ -2224,14 +2230,17 @@ We can check our code for consistency by running:
2224
2230
2225
2231
```bash
2226
2232
$ bin/rubocop
2233
+
```
2234
+
2235
+
This will print out any offenses and tell you what they are.
0 commit comments