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

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.

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 ]