title: Why does my Rails AJAX delete method only work if I refresh?
body: <p>I just incorporated the DESTROY method for items in my school project. It worked fine, but now I must use AJAX to complete the action. After implementing this code, it only displays on the browser when I refresh the page, and not instantly when I delete an item. If I did not include enough information please let me know.</p>
  
  <p><strong>_item.html.erb</strong></p>
  
  <pre><code>&lt;% item.each do |i| %&gt;
    &lt;p&gt;&lt;%= i.name %&gt; | &lt;%= link_to "Complete", i, method: :delete, remote: true, class: 'glyphicon glyphicon-ok' %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>destroy.js.erb</strong></p>
  
  <pre><code>&lt;% if @item.destroyed? %&gt;
    $('#item-' +&lt;%= @item.id %&gt;).hide();
  &lt;% else %&gt;
    $('#item-' +&lt;%= @item.id %&gt;).prepend("&lt;%= flash[:error] %&gt;");
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>items_controller.rb</strong></p>
  
  <pre><code>class ItemsController &lt; ApplicationController
    def index
      @items = Item.all
    end
  
    def show
      @item = Item.find(params[:id])
    end
  
    def new
      @item = Item.new
    end
  
    def edit
      @item = Item.find(params[:id])
    end
  
    def create
      @item = Item.new(params.require(:item).permit(:name))
  
      if @item.save
        flash[:notice] = "The item was added to your list."
        redirect_to current_user
      else
        flash[:error] = "There was a problem creating your item."
        redirect_to current_user
      end
    end
  
    def destroy
      @item = Item.find(params[:id])
  
      if @item.destroy
        flash[:notice] = "\"#{@item.name}\" was completed and destroyed."
      else
        flash[:error] = "There was an error completing the item."
      end
  
      respond_to do |format|
        format.html
        format.js
      end
  
    end
  
  end
  </code></pre>