title: Ajax with Polymorphic Association (Destroy & Create)
body: <p><strong>I have a polymorphic association with dailyposts &amp; comments in my rails app</strong></p>
  
  <p>Everything works fine in the database, but when I try to add Ajax to the Destroy Action (I used this tutorial <a href="http://railscasts.com/episodes/136-jquery-ajax-revised" rel="nofollow">http://railscasts.com/episodes/136-jquery-ajax-revised</a>)
  ....it doesn't  work unless I refresh the page. (but my alert works in destroy.js.erb)</p>
  
  <p><strong>I know my error is in destroy.js.erb</strong></p>
  
  <p>New to Rails...Please help :)</p>
  
  <p><em>This is my code...</em></p>
  
  <p><strong>ROUTES</strong></p>
  
  <pre><code>resources :dailyposts do
    resources :comments
  end
  </code></pre>
  
  <p><strong>CONTROLLERS</strong></p>
  
  <pre><code>##Dailyposts
  
  class DailypostsController &lt; ApplicationController
  
    respond_to :html, :js
  
    def show
      @user = User.find_by_username(params[:username])
      @dailypost = Dailypost.find_by_id(params[:id])
      @commentable = @dailypost
      @comments = @commentable.comments.arrange(:order =&gt; :created_at)
      @comment = Comment.new
    end
  end
  
  ##Comments
  
  class CommentsController &lt; ApplicationController
    before_filter :load_commentable
  
    respond_to :html, :js
  
    def create
      @comment = @commentable.comments.create(params[:comment])
      @comment.user = current_user
      if @comment.save
        respond_to do |format|
          format.html { redirect_to @commentable }
          format.js
        end
      else
        redirect_to @commentable, notice: "Comment can't be blank."
      end
    end
  
    def destroy
      @comment = Comment.find(params[:id])
      @commentable = @comment.commentable
      if @comment.destroy
        respond_to do |format|
          format.html { redirect_to @commentable }
          format.js
        end
      end
    end
  
    private 
  
    def load_commentable
      resource, id = request.path.split('/')[1, 2]
      @commentable = resource.singularize.classify.constantize.find(id)
    end
  
  end
  </code></pre>
  
  <p><strong>VIEWS</strong></p>
  
  <h2>Dailypost show.html.erb</h2>
  
  <pre><code>  &lt;div class="row-fluid"&gt;
      &lt;div class="span12"&gt;
  
        &lt;div&gt;
          &lt;%= raw(dailypost_with_links(@dailypost)) %&gt;
        &lt;/div&gt;
  
        &lt;%= render "comments/form" %&gt;
  
        &lt;div id="comments"&gt;         
          &lt;%= nested_comments @comments %&gt;
        &lt;/div&gt;
  
      &lt;/div&gt;
  
    &lt;/div&gt;
  </code></pre>
  
  <h2>_comment.html.erb</h2>
  
  <pre><code>&lt;section class="comments"&gt;
    &lt;div class ="user"&gt;
      &lt;%= link_to comment.user.username, comment.user  %&gt; 
      &lt;%= comment.content %&gt;
    &lt;/div&gt;
  
  ##Here I am passing remote: true for ajax
  
      &lt;% if current_user?(comment.user) %&gt; 
        &lt;%= link_to content_tag(:i, "", class: "icon-trash icons"), [@commentable, comment], method: :delete,
                         data: { confirm: "Are you sure?" },
                         title: "Delete", remote: true %&gt; |
      &lt;% end %&gt;
  
  &lt;/section&gt;
  </code></pre>
  
  <h2>destroy.js.erb</h2>
  
  <pre><code>##alert is working
  alert('ajax works!');
  
  $('#&lt;%= dom_id(@comment) %&gt;').remove();
  </code></pre>
  
  <p><strong>LOGS</strong></p>
  
  <pre><code>Started DELETE "/dailyposts/11/comments/133" for 127.0.0.1 at 2013-08-04 23:06:31 -0700
  Processing by CommentsController#destroy as JS
    Parameters: {"dailypost_id"=&gt;"11", "id"=&gt;"133"}
    Dailypost Load (0.3ms)  SELECT "dailyposts".* FROM "dailyposts" WHERE "dailyposts"."id" = ? ORDER BY dailyposts.created_at DESC LIMIT 1  [["id", "11"]]
    User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'sgFH2XeZWEXCcjxiAwgfXg' LIMIT 1
    Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1  [["id", "133"]]
    Dailypost Load (0.1ms)  SELECT "dailyposts".* FROM "dailyposts" WHERE "dailyposts"."id" = 11 ORDER BY dailyposts.created_at DESC LIMIT 1
     (0.1ms)  begin transaction
    Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE (comments.ancestry like '133/%' or comments.ancestry = '133')
    SQL (0.4ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 133]]
     (2.3ms)  commit transaction
    Rendered comments/destroy.js.erb (0.7ms)
  Completed 200 OK in 25ms (Views: 10.3ms | ActiveRecord: 3.9ms)
  </code></pre>