small medium large xlarge

10 Jan 2011, 20:18
ubique (11 posts)

I complete Chapter 10 and run a ‘rake test’ and get no errors or failures. When I go to complete Chapter 11 I get all sorts of errors after following the book and have restarted the chapter several times now. but to no avail. Am not a programmer but trying to learn ROR. Can anyone help here….

PS - I have restarted my server several times to no joy.

Rake test: results from Terminal
` 1) Failure: test_should_destroy_cart(CartsControllerTest) [/test/functional/carts_controller_test.rb:43]: “Cart.count” didn’t change by -1. <1> expected but was <2>. `

2) Error: test_should_create_line_item(LineItemsControllerTest): NoMethodError: undefined method price=’ for # app/models/cart.rb:18:in `add_product' app/controllers/line_items_controller.rb:53:in `create' /test/functional/line_items_controller_test.rb:29:in `test_should_create_line_item' /test/functional/line_items_controller_test.rb:28:in `test_should_create_line_item'. `

` 3) Error: test_should_create_line_item_via_ajax(LineItemsControllerTest): NoMethodError: undefined method price=' for #<LineItem:0x10344a0f8> app/models/cart.rb:18:in add_product’ app/controllers/line_items_controller.rb:53:in create' /test/functional/line_items_controller_test.rb:59:in test_should_create_line_item_via_ajax’ /test/functional/line_items_controller_test.rb:58:in test_should_create_line_item_via_ajax' 23 tests, 32 assertions, 1 failures, 2 errors Errors running test:functionals!

11 Jan 2011, 01:00
Sam Ruby (633 posts)

It would be helpful if you posted what your code looks like, in particular I would like to see the source to app/models/cart.rb.

My best guess as to what is going on here: there is an optional playtime exercise at the end of iteration E3 that adds a price column to the line item table. Either this exercise wasn’t completed successfully, or it was skipped. Skipping would be OK, but if you were to mix in code downloaded from (which includes this exercise completed successfully) you would see this error.

If it helps, you can download the necessary migration.

11 Jan 2011, 13:01
ubique (11 posts)


This is my cart.rb file as of end Ch.10 with no failures or errors.

Its different to both your versions, which is worrying!!

Out of interest which is the most up-to-date - should I be using the book (I have V10) or the exercise code files?

` class Cart < ActiveRecord::Base has_many :line_items, :dependent => :destroy

def add_product(product_id) current_item = line_items.where(:product_id => product_id).first if current_item current_item.quantity += 1 else current_item = => product_id) end current_item end

def total_price line_items.to_a.sum { |item| item.total_price } end end `

11 Jan 2011, 13:46
Sam Ruby (633 posts)

In order to help debug a problem, I would need to see code that exhibits the symptom.

I continually test the scenario in the book against the latest version of Rails and publish the results. Each time a beta is published, the latest version of the tested scenario is placed on the site.

The only difference between what is published and what you have should be in the choice of which optional exercises have been done. It is OK to skip these exercises, but just don’t mix and match. As an example, the traceback above indicates that you may have been trying to set the price on a line item without having first added a price column to the line item table.

12 Jan 2011, 02:26
ubique (11 posts)

Thanks for your reply. I save out every Chapter once its completed when the ‘rake test’ comes back with ‘0’ errors and failures and the browser works as shown in the book. I haven’t tried the extra exercises at the end of each chapter because I’m not confident enough yet. So what is that is going so wrong with Ch11 that everything breaks including the Ajax function? If I am having this issue, then I know the other students in my class are about to have it too and have spent a huge amount of time on this already.

1 example of a few I notice is - line 10:

` Ch 9 depot/app/models/product.rb

class Product < ActiveRecord::Base default_scope :order => ‘title’ has_many :line_items

before_destroy :ensure_not_referenced_by_any_line_item

# ensure that there are no line items referencing this product def ensure_not_referenced_by_any_line_item if return true else errors.add(:base, ‘Line Items present’) return false end end `

Ch 9 depot _f/exercise file says ` class Product < ActiveRecord::Base default_scope :order => ‘title’ has_many :line_items

before_destroy :ensure_not_referenced_by_any_line_item

# ensure that there are no line items referencing this product def ensure_not_referenced_by_any_line_item if return true else errors[:base] « “Line Items present” return false end end `

Is the book right or the exercise files right as of today?

Which should we be following to get the shopping cart working correctly?

12 Jan 2011, 20:11
Sam Ruby (633 posts) contains errors.add(:base, 'Line Items present').

Beta 12 (dated January 10, 2011), page 124 contains errors.add(:base, 'Line Items present').

Beta 11 (dated November 24, 2010), page 124 contains errors.add(:base, 'Line Items present').

Beta 10 (dated October 28, 2010), page 123 contains errors[:base] << "Line Items present".

Which beta are you using? This information is contained in the bottom right hand corner of every page.

13 Jan 2011, 11:34
ubique (11 posts)

Okay - am off to start this book once again…

Will use the latest code download and the book as a guide - ‘cos I printed it out so I could follow it along!

You must be logged in to comment