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 Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
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
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
AstonJ
Or looking forward to? :nerd_face:
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
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
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
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in PragProg Customers

View all threads ❯