small medium large xlarge

Generic-user-small
18 May 2017, 17:04
Ken Beegle (3 posts)

Chapter 13 ends with adding a pay_type_params method but from what I can see, it doesn’t appear to be called by any other methods in the orders controller. The same goes with the sample code used throughout the later chapters. I’m still in the beginner/intermediate learning phase of ruby so it’s very possible I’m not seeing something.

Also, the method uses a series of if, elsif statements. Does it make sense to swap that out for a case statement, something along the lines of:

def pay_type_params
  permitted_pay_type_params = case order_params[:pay_type]
  when 'Credit Card'     then [:credit_card_number, :expiration_date]
  when 'Check'           then [:routing_number, :account_number]
  when 'Purchase Order'  then [:po_number]
  end

  params.require(:order).permit(permitted_pay_type_params)
end

My code drops the final else {} but I wasn’t sure how the method was being called or if Hash would be empty expected if no pay_type_params.

Dmfcb_pragsmall
18 May 2017, 19:26
David Copeland (436 posts)

Yes, we do plan on using that method in a later chapter, so in the final product you’ll see in this chapter a foreward link to where we’ll use it and then in a following chapter some actual use of it. It’s bit odd now in the beta.

I’ll also think about your case statement idea. That does look a little cleaner to me. I was initially thinking that if/else would be easier for those new to Ruby, but maybe not?

Generic-user-small
18 May 2017, 19:44
Ken Beegle (3 posts)

Thank you for the heads up on future use of the method.

After looking at the code again, I had second thoughts on the case method. A case statement may be just a tiny bit cleaner but two reasons to stay with the if/elsif syntax are that it matches the flow of the react code on page 210 a bit better and I did a quick search through the book and you don’t use a case statement anywhere else.

Also, just a big thank you for all the versions of the book that have been published. I’ve purchased a number of them and learned from them each time and will continue to do so in the future.

You must be logged in to comment