small medium large xlarge

06 Mar 2012, 14:25
Xavier John (21 posts)

The following test “should get new” on orders_controller_test is failing.

ActionView::Template::Error: undefined method *' for nil:NilClass app/models/line_item.rb:6:in total_price’ app/views/line_items/_line_item.html.erb:8:in _app_views_line_items__line_item_html_erb___425113360_36076368' app/views/carts/_cart.html.erb:3:in _app_views_carts__cart_html_erb__276269900_36564168’ app/views/layouts/application.html.erb:23:in block in _app_views_layouts_application_html_erb__98986146_40318308' app/helpers/application_helper.rb:6:in hidden_div_if’ app/views/layouts/application.html.erb:22:in _app_views_layouts_application_html_erb__98986146_40318308' app/controllers/orders_controller.rb:34:in new’ test/functional/orders_controller_test.rb:25:in `block in '

The test is copied from the book.

test “should get new” do cart = Cart.create session[:cart_id] = LineItem.create(cart: cart, product: products(:ruby))

get :new
assert_response :success   end

class LineItem < ActiveRecord::Base belongs_to :product belongs_to :cart

def total_price price * quantity end end

What am I missing?


13 Mar 2012, 02:19
Dave Thomas (390 posts)

The error is saying you’re multiplying @nil@ by something (undefined method “*” for nil). And it looks like you’re doing price * quantity. So what is price? Is it set to something?

13 Mar 2012, 15:57
Xavier John (21 posts)

price is a column in the line_items table.

I have verified that there are no null data in the price column.

There is code in application.html.erb that avoids calling a function if card is not set.

    <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
            <%= render @cart %>
        <% end %>
    <% end %>

but what if the cart is set but no line_item is created, wouldn’t that cause the error I am getting?

Slightly off topic. Is it just by convention ActiveRecord maps class LineItem to line_items? Does it figure out the columns dynamically?

27 Mar 2012, 08:44
Xavier John (21 posts)

How do I make this line in unit test LineItem.create(cart: cart, product: products(:ruby)) also add the product price to LineItem?

The code works via the website but it fails in unit test because price is Nil. If I change the code def total_price price * quantity end to def total_price product.price * quantity end it works but I am trying to use the price in the lineitem. It is a Playtime work item in a previous chapter.

29 Jun 2012, 09:19
Dalius Gilvydis (1 post)

I’m having a very similar problem, though I discovered mine in Chapter 13, section 13.1, when testing the Order Notifier emails ( order_notifier_test.rb ).

I should note that I as well made the changes to the line_item table so that it includes a “price” as well (separate from “price” in the product table). This was an exercise in Playtime (chapter 10 - A Smarter Cart).

My program functions perfectly in development mode. The “received” and the “shipped” emails are sent off and look great. The “total price” is calculated the way it should (using “price” from the line_items table).

I run into the “undefined method `*’ for nil:NilClass” problem only in test mode, when running

@ruby -I test test/functional/order_notifier_test.rb@

As Xavier suggested, I changed

@price * quantity@


@product.price * quantity@

and the error went away. I’d like to stick to using the price originally set in the line_items table. But more importantly, I’d like to understand why this is happening.

I’ve been trying to track down the source of this problem all day, but can’t seem to figure it out. Anybody have any ideas? I’d really like to understand why the application works great, but the test fails.

Any help would be appreciated!

21 Sep 2012, 12:19
Ben Wallis (1 post)

Having exactly the same issue - did you guys find a solution?

You must be logged in to comment