small medium large xlarge


Activity 1

Get HTML, XML, and JSON formatted views working for who_bought requests. Experiment with including the order information in the XML view by rendering @product.to_xml(include: :orders). Do the same thing for JSON.

Hint: use builder templates for XML.


Activity 2

What happens if you click the “Checkout” button in the sidebar while the checkout screen is already displayed? Can you find a way to disable the button in this circumstance?

Hint: variables set in the controller are available in layouts and partials as well as in the directly rendered template.


Activity 3

The list of possible payment types is currently stored as a constant in the Order class. Can you move this list into a database table? Can you still make validation work for the field?

Hint: Replace the pay_type string with a pay_type_id in the Orders model, and use to verify that this field is set.


Other Discussion

George Says

I ran into below bizzare behavior, can anyone shed some light?

        # The only difference between below two is the space between "redirect_to"  and  "(store_url" 
        #format.html { redirect_to (store_url, :notice => 'Thank you for your order' ) }  #don't work??
        #format.html { redirect_to(store_url, :notice => 'Thank you for your order.') }   #works!!

### error in broswer
C:/wug/proj_RoR/depot/app/controllers/orders_controller.rb:63: syntax error, unexpected ',', expecting ')'
...t.html {redirect_to (store_url, :notice => 'Thank you for yo...
...                               ^
C:/wug/proj_RoR/depot/app/controllers/orders_controller.rb:63: syntax error, unexpected ')', expecting '}'
...=> 'Thank you for your order.') }  
...                               ^

Mike says

redirect_to(...) is a function call with the arguments enclosed in the parens.

redirect_to (...) is a function call with one argument. The argument is enclosed in parens, but is syntactically incorrect: (foo, bar) is not a legal expression.

Michael says

Did anyone else have a problem with the orders_controllers_test.rb for test “should get new”? (p. 160 of the book).

The line

LineItem.create(:cart => cart, :product => products(:ruby))
ends up causing an error because price never gets filled in on the line_item, because, as far as I can tell the line_items_controller#create method never gets called, which is the only method that calls the Cart#add_product method, which is the one that actually fills in price on a line_item.

Here’s the full error from the unit test:
 1) Error:
ActionView::Template::Error: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.*
    /home/midpeter444/ruby/rails/depot/app/models/line_item.rb:24:in `total_price'
So the fix for me was to do instead:
LineItem.create(:cart => cart, :product => products(:ruby), :price => products(:ruby).price)

If I’m doing something stupid, please let me know. Maybe there’s a better way to solve this.

Sam says Duplicating line item fixtures?

I want to ask that on page 152, two line item fixtures were created, but they are the same book and belongs to the same cart. Wouldn’t this be a issue?

Page History
  • V27: Denis Hovart [over 1 year ago]
  • V26: eric tenne [about 2 years ago]
  • V25: George Buckingham [over 4 years ago]
  • V24: William Ko [over 5 years ago]
  • V23: William Ko [over 5 years ago]
  • V22: William Ko [over 5 years ago]
  • V21: William Ko [over 5 years ago]
  • V20: William Ko [over 5 years ago]
  • V19: Sam Ruby [over 6 years ago]
  • V18: Sam Ruby [over 6 years ago]