25 Mar 2014, 19:51

kevin lau (3 posts)


Im stuck on this error. Anyone have a fix?

1) Failure: LineItemsControllerTest#test_should_create_line_item_via_ajax [/Users/KevinLau/Documents/rails/LRE/test/controllers/line_items_controller_test.rb:55]: Expected at least 1 element matching “tr#current_item td”, found 0.. Expected 0 to be >= 1.

Thanks in advance.

26 Mar 2014, 21:26

Angela Gordon (2 posts)

Hmm. Its working for me. Maybe check your _line_item.html.erb file to see if you have a typo when defining the <tr id="current_item"> line? Wish I could be of more help. Maybe its just a simple misspelling?

27 Mar 2014, 16:00

kevin lau (3 posts)


Thank for your response, I still can’t track down what the issue is.

<% if line_item == @current_items %> <tr id="current_item"> <% else %>

<% end %> <%= line_item.quantity %>× <%= line_item.product.title %> <%= number_to_currency(line_item.total_price) %>
 def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url }
        format.js   { @current_item = @line_item }
        format.json { render action: 'show', status: :created, location: @line_item }
        format.html { render action: 'new' }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }

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


if ($('#cart tr').length > -1) { $('#cart').show('blind', 1000); }

$('#cart').html("<%= escape_javascript render(@cart) %>");

   animate({'background-color':'#114411'}, 1000);


I’m having issues with the Ajax displaying the cart when there is an item as well.

27 Mar 2014, 16:11

kevin lau (3 posts)

Expected at least 1 element matching "tr#current_item td", found 0..
Expected 0 to be >= 1.

Does it mean it can’t find the “current_item” element or the actual item?

Thanks again.

27 Mar 2014, 16:52

Angela Gordon (2 posts)

My code is almost the same with the exception of:

You have if ($(‘#cart tr’).length > -1) { $(‘#cart’).show(‘blind’, 1000); } I have if ($(‘#cart tr’).length == 1) { $(‘#cart’).show(‘blind’, 1000);}

You should only have to show the cart once there is one item in it, then it remains visible.


You have $(‘#notice’).hide(); and I don’t have that line at all since I am not sending any notices when some one adds an item to the cart.

But those probably aren’t the source of your problem.

So when you test manually, using the browser, are you saying the side bar never shows you the items you add to the cart? Maybe the javascript isn’t even being executed? Maybe check the naming and placement of your javascript erb file: it should be named create.js.erb and located under app/views/line_items directory If you do have it properly named and in the right place then maybe try checking out the javascript console logs or your rails logs for any errors..

  You must be logged in to comment