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 ]

Where Next?

Popular Pragmatic Bookshelf topics Top

jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
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
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
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

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
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
New

Latest in Agile Web Development with Rails 7

Agile Web Development with Rails 7 Portal

Sub Categories: