@@ -1583,9 +1583,9 @@ To log out of the application, we can add a button to the top of
1583
1583
you want to include in every page like a header or footer.
1584
1584
1585
1585
Add a small ` <nav> ` section inside the ` <body> ` with a link to Home and a Log
1586
- out button.
1586
+ out button and wrap ` yield ` with a ` <main> ` tag .
1587
1587
1588
- ``` erb#5-8
1588
+ ``` erb#5-8,10,12
1589
1589
<!DOCTYPE html>
1590
1590
<html>
1591
1591
<!-- ... -->
@@ -1595,7 +1595,9 @@ out button.
1595
1595
<%= button_to "Log out", session_path, method: :delete if authenticated? %>
1596
1596
</nav>
1597
1597
1598
- <%= yield %>
1598
+ <main>
1599
+ <%= yield %>
1600
+ </main>
1599
1601
</body>
1600
1602
</html>
1601
1603
```
@@ -1710,96 +1712,6 @@ to ensure that it never renders stale cache data.
1710
1712
1711
1713
Learn more in the [ Caching with Rails] ( caching_with_rails.html ) guide.
1712
1714
1713
- Adding CSS & JavaScript
1714
- -----------------------
1715
-
1716
- CSS & JavaScript are core parts of building web applications, so let's learn how
1717
- to use them with Rails.
1718
-
1719
- ### Propshaft
1720
-
1721
- Rails' asset pipeline is called Propshaft. It takes your CSS, JavaScript,
1722
- images, and other assets and serves them to your browser. In production,
1723
- Propshaft keeps track of each version of your assets so they can be cached to
1724
- make your pages faster. Check out the
1725
- [ Asset Pipeline guide] ( asset_pipeline.html ) to learn more about how this works.
1726
-
1727
- Let's modify ` app/assets/stylesheets/application.css ` and change our font to
1728
- sans-serif.
1729
-
1730
- ``` css
1731
- body {
1732
- font-family : Arial , Helvetica , sans-serif ;
1733
- }
1734
-
1735
- nav {
1736
- display : flex ;
1737
- gap : 0.5rem ;
1738
- }
1739
-
1740
- img {
1741
- max-width : 800px ;
1742
- }
1743
-
1744
- .notice {
1745
- color : green ;
1746
- }
1747
- ```
1748
-
1749
- Refresh your page and you'll see the CSS has been applied.
1750
-
1751
- ### Import Maps
1752
-
1753
- Rails uses import maps for JavaScript by default. This allows you to write
1754
- modern JavaScript modules with no build steps.
1755
-
1756
- You can find the JavaScript pins in ` config/importmap.rb ` . This file maps the
1757
- JavaScript package names with the source file which is used to generate the
1758
- importmap tag in the browser.
1759
-
1760
- ``` ruby
1761
- # Pin npm packages by running ./bin/importmap
1762
-
1763
- pin " application"
1764
- pin " @hotwired/turbo-rails" , to: " turbo.min.js"
1765
- pin " @hotwired/stimulus" , to: " stimulus.min.js"
1766
- pin " @hotwired/stimulus-loading" , to: " stimulus-loading.js"
1767
- pin_all_from " app/javascript/controllers" , under: " controllers"
1768
- ```
1769
-
1770
- TIP: Each pin maps a JavaScript package name (e.g., ` "@hotwired/turbo-rails" ` )
1771
- to a specific file or URL (e.g., ` "turbo.min.js" ` ). ` pin_all_from ` maps all
1772
- files in a directory (e.g., ` app/javascript/controllers ` ) to a namespace (e.g.,
1773
- ` "controllers" ` ).
1774
-
1775
- Import maps keep the setup clean and minimal, while still supporting modern
1776
- JavaScript features.
1777
-
1778
- What are these JavaScript files already in our import map? They are a frontend
1779
- framework called Hotwire that Rails uses by default.
1780
-
1781
- ### Hotwire
1782
-
1783
- Hotwire is a JavaScript framework designed to take full advantage of server-side
1784
- generated HTML. It is comprised of 3 core components:
1785
-
1786
- 1 . [ ** Turbo** ] ( https://turbo.hotwired.dev/ ) handles navigation, form
1787
- submissions, page components, and updates without writing any custom
1788
- JavaScript.
1789
- 2 . [ ** Stimulus** ] ( https://stimulus.hotwired.dev/ ) provides a framework for when
1790
- you need custom JavaScript to add functionality to the page.
1791
- 3 . [ ** Native** ] ( https://native.hotwired.dev/ ) allows you to make hybrid mobile
1792
- apps by embedding your web app and progressively enhancing it with native
1793
- mobile features.
1794
-
1795
- We haven't written any JavaScript yet, but we have been using Hotwire on the
1796
- frontend. For instance, the form you created to add and edit a product was
1797
- powered by Turbo.
1798
-
1799
- Learn more in the [ Asset Pipeline] ( asset_pipeline.html ) and
1800
- [ Working with JavaScript in Rails] ( working_with_javascript_in_rails.html )
1801
- guides.
1802
-
1803
1715
Rich Text Fields with Action Text
1804
1716
---------------------------------
1805
1717
@@ -2512,6 +2424,143 @@ without raising any errors.
2512
2424
Use the Rails console to send another email and test the unsubscribe link in the
2513
2425
logs.
2514
2426
2427
+ Adding CSS & JavaScript
2428
+ -----------------------
2429
+
2430
+ CSS & JavaScript are core to building web applications, so let's learn how to
2431
+ use them with Rails.
2432
+
2433
+ ### Propshaft
2434
+
2435
+ Rails' asset pipeline is called Propshaft. It takes your CSS, JavaScript,
2436
+ images, and other assets and serves them to your browser. In production,
2437
+ Propshaft keeps track of each version of your assets so they can be cached to
2438
+ make your pages faster. Check out the
2439
+ [ Asset Pipeline guide] ( asset_pipeline.html ) to learn more about how this works.
2440
+
2441
+ Let's modify ` app/assets/stylesheets/application.css ` and change our font to
2442
+ sans-serif.
2443
+
2444
+ ``` css
2445
+ body {
2446
+ font-family : Arial , Helvetica , sans-serif ;
2447
+ padding : 1rem ;
2448
+ }
2449
+
2450
+ nav {
2451
+ justify-content : flex-end ;
2452
+ display : flex ;
2453
+ font-size : 0.875em ;
2454
+ gap : 0.5rem ;
2455
+ max-width : 1024px ;
2456
+ margin : 0 auto ;
2457
+ padding : 1rem ;
2458
+ }
2459
+
2460
+ nav a {
2461
+ display : inline-block ;
2462
+ }
2463
+
2464
+ main {
2465
+ max-width : 1024px ;
2466
+ margin : 0 auto ;
2467
+ }
2468
+
2469
+ .notice {
2470
+ color : green ;
2471
+ }
2472
+
2473
+ section .product {
2474
+ display : flex ;
2475
+ gap : 1rem ;
2476
+ flex-direction : row ;
2477
+ }
2478
+
2479
+ section .product img {
2480
+ border-radius : 8px ;
2481
+ flex-basis : 50% ;
2482
+ max-width : 50% ;
2483
+ }
2484
+ ```
2485
+
2486
+ Then we'll update ` app/views/products/show.html.erb ` to use these new styles.
2487
+
2488
+ ``` erb#1,3,6,18-19
2489
+ <p><%= link_to "Back", products_path %></p>
2490
+
2491
+ <section class="product">
2492
+ <%= image_tag @product.featured_image if @product.featured_image.attached? %>
2493
+
2494
+ <section class="product-info">
2495
+ <% cache @product do %>
2496
+ <h1><%= @product.name %></h1>
2497
+ <%= @product.description %>
2498
+ <% end %>
2499
+
2500
+ <%= render "inventory", product: @product %>
2501
+
2502
+ <% if authenticated? %>
2503
+ <%= link_to "Edit", edit_product_path(@product) %>
2504
+ <%= button_to "Delete", @product, method: :delete, data: { turbo_confirm: "Are you sure?" } %>
2505
+ <% end %>
2506
+ </section>
2507
+ </section>
2508
+ ```
2509
+
2510
+ Refresh your page and you'll see the CSS has been applied.
2511
+
2512
+ ### Import Maps
2513
+
2514
+ Rails uses import maps for JavaScript by default. This allows you to write
2515
+ modern JavaScript modules with no build steps.
2516
+
2517
+ You can find the JavaScript pins in ` config/importmap.rb ` . This file maps the
2518
+ JavaScript package names with the source file which is used to generate the
2519
+ importmap tag in the browser.
2520
+
2521
+ ``` ruby
2522
+ # Pin npm packages by running ./bin/importmap
2523
+
2524
+ pin " application"
2525
+ pin " @hotwired/turbo-rails" , to: " turbo.min.js"
2526
+ pin " @hotwired/stimulus" , to: " stimulus.min.js"
2527
+ pin " @hotwired/stimulus-loading" , to: " stimulus-loading.js"
2528
+ pin_all_from " app/javascript/controllers" , under: " controllers"
2529
+ ```
2530
+
2531
+ TIP: Each pin maps a JavaScript package name (e.g., ` "@hotwired/turbo-rails" ` )
2532
+ to a specific file or URL (e.g., ` "turbo.min.js" ` ). ` pin_all_from ` maps all
2533
+ files in a directory (e.g., ` app/javascript/controllers ` ) to a namespace (e.g.,
2534
+ ` "controllers" ` ).
2535
+
2536
+ Import maps keep the setup clean and minimal, while still supporting modern
2537
+ JavaScript features.
2538
+
2539
+ What are these JavaScript files already in our import map? They are a frontend
2540
+ framework called Hotwire that Rails uses by default.
2541
+
2542
+ ### Hotwire
2543
+
2544
+ Hotwire is a JavaScript framework designed to take full advantage of server-side
2545
+ generated HTML. It is comprised of 3 core components:
2546
+
2547
+ 1 . [ ** Turbo** ] ( https://turbo.hotwired.dev/ ) handles navigation, form
2548
+ submissions, page components, and updates without writing any custom
2549
+ JavaScript.
2550
+ 2 . [ ** Stimulus** ] ( https://stimulus.hotwired.dev/ ) provides a framework for when
2551
+ you need custom JavaScript to add functionality to the page.
2552
+ 3 . [ ** Native** ] ( https://native.hotwired.dev/ ) allows you to make hybrid mobile
2553
+ apps by embedding your web app and progressively enhancing it with native
2554
+ mobile features.
2555
+
2556
+ We haven't written any JavaScript yet, but we have been using Hotwire on the
2557
+ frontend. For instance, the form you created to add and edit a product was
2558
+ powered by Turbo.
2559
+
2560
+ Learn more in the [ Asset Pipeline] ( asset_pipeline.html ) and
2561
+ [ Working with JavaScript in Rails] ( working_with_javascript_in_rails.html )
2562
+ guides.
2563
+
2515
2564
Testing
2516
2565
-------
2517
2566
0 commit comments