s2k

s2k

Author of Fast Feedback Using Ruby

Agile Web Development with Rails 7: Code Example for `set_cart` (page 119)

For consistency within the LineItemsController, who about using the same notation in the before filters:

class LineItemsController < ApplicationController
  include CurrentCart
  before_action :set_cart, only: %i[create]
  before_action :set_line_item, only: %i[show edit update destroy]

Notice,I replaced only [:create] with only %i[create].
It’s slightly more typing, but improves consistency in code formatting with the following line, that also uses the %i[…] notation.

Since this involves only one action, it may be even better to use only: :create, or even call set_cart fron inside the (only) action that uses it.

Marked As Solved

rubys

rubys

Author of Agile Web Development With Rails

Thanks for catching this! I want the code you add to be consistent with the generated scaffolding, and the generated scaffolding changed in a way that wasn’t caught by my automated testing, and I missed this in my review.

My goal is not just to be consistent within an individual controller, but across controllers. Accordingly, I’ve made changes so that the following is what you end up with at the end of the book:

./app/controllers/line_items_controller.rb:  skip_before_action :authorize, only: %i[ create ]
./app/controllers/line_items_controller.rb:  before_action :set_cart, only: %i[ create ]
./app/controllers/line_items_controller.rb:  before_action :set_line_item, only: %i[ show edit update destroy ]
./app/controllers/carts_controller.rb:  skip_before_action :authorize, only: %i[ create update destroy ]
./app/controllers/carts_controller.rb:  before_action :set_cart, only: %i[ show edit update destroy ]
./app/controllers/orders_controller.rb:  skip_before_action :authorize, only: %i[ new create ]
./app/controllers/orders_controller.rb:  before_action :set_cart, only: %i[ new create ]
./app/controllers/orders_controller.rb:  before_action :ensure_cart_isnt_empty, only: %i[ new ]
./app/controllers/orders_controller.rb:  before_action :set_order, only: %i[ show edit update destroy ]
./app/controllers/products_controller.rb:  before_action :set_product, only: %i[ show edit update destroy ]
./app/controllers/users_controller.rb:  before_action :set_user, only: %i[ show edit update destroy ]
./config/routes.rb:  resources :support_requests, only: %i[ index update ]

Popular Prag Prog topics Top

brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

Other popular topics Top

New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New