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

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
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
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
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
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
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

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
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
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New

Latest in PragProg

View all threads ❯