title: Ruby on Rails rendering object with ajax
body: <p>I am following the book Agile Web Development with Rails 5.1 and am in Chapter 11 where I am trying to create a button that decrements the quantity of an item within my cart. I am trying to use ajax to solve this and calling the render method on the cart. I am getting the following error...</p>
  
  <p>ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
      1: cart = document.getElementById("cart")
      2: cart.innerHTML = "&lt;%= j render(@cart) %>"</p>
  
  <p>the button that this ajax calls originates from is a partial if that changes anything. I am new to rails and am stuck.</p>
  
  <p>decrement_quantity.js.coffee</p>
  
  <pre><code>cart = document.getElementById("cart")
  cart.innerHTML = "&lt;%= j render(@cart) %&gt;"
  </code></pre>
  
  <p>_line_item.html.erb</p>
  
  <pre><code>&lt;% if line_item== @current_item %&gt;
  &lt;tr class="line-item-highlight"&gt;
  &lt;% else %&gt;
  &lt;tr&gt;
  &lt;% end %&gt;
    &lt;td id="quantity"&gt;&lt;%= line_item.quantity %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= line_item.product.title %&gt;&lt;/td&gt;
    &lt;td id="price" class="price"&gt;&lt;%= number_to_currency(line_item.total_price) %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= button_to 'X', line_item, method: :delete, data: {confirm: 'Are you sure?' } %&gt;&lt;/td&gt;
    &lt;td id="decrement_quantity"&gt;&lt;%= button_to '-1', line_items_decrement_quantity_path(id: line_item.id), remote: true%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>line_items_controller.rb</p>
  
  <pre><code>class LineItemsController &lt; ApplicationController
    include CurrentCart
    before_action :set_cart, only: [:create]
    before_action :set_line_item, only: [:show, :edit, :update, :destroy, :decrement_quantity]
  .
  .
  .
    def decrement_quantity
      if @line_item.quantity &gt; 1
        @line_item.quantity -= 1
        @line_item.save
      else
        @line_item.destroy
      end
      respond_to do |format|
        format.html { redirect_to store_index_url, notice: 'Line item was successfully decremented.' }
        format.js {}
        format.json { head :no_content }
      end
    end
  .
  .
  .
  
  </code></pre>
  
  <p>routes.db</p>
  
  <pre><code>Rails.application.routes.draw do
    resources :line_items
    resources :carts
    #get 'store/index'
    root 'store#index', as: 'store_index'
  
    post 'line_items/decrement_quantity'
  
    resources :products
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  end
  </code></pre>
  
  <p>Im not sure if im missing something but the same ajax command from from a different button in a different view is working fine. Anyone to help guide me is much appreciated :)</p>