title: Rails - AJAX/JQuery not working
body: <p>For my application, I have projects where users can make comment (class Newcomments) postings. Right now it posts great but on page refresh. I am trying to use AJAX/JQUERY so that it will post with no page refresh. I am following this <a href="http://railscasts.com/episodes/136-jquery-ajax-revised?view=asciicast" rel="nofollow">railscasts</a> tutorial. </p>
  
  <p>So right now, the posts are made to my database and the page does not refresh. But when I refresh, the posts shows up. The page that I render my comments for a specific project is on the projects/_comments.html.erb.</p>
  
  <p><strong>Question: How would I adjust it so that it the new comment made renders?</strong></p>
  
  <p><strong>newcomments_controller.rb</strong></p>
  
  <pre><code>def create
    @newcomment = @commentable.newcomments.new(params[:newcomment])
  
    if @newcomment.save   
           respond_to do |format|
             format.html { redirect_to comments_project_path(@commentable) }
             format.js
           end
    else
       render :new
    end
  end
  </code></pre>
  
  <p><strong>view/newcomments/_form.html.erb</strong></p>
  
  <pre><code>&lt;span class="comment"&gt;
  &lt;%= form_for [@commentable, @newcomment], remote: true do |f| %&gt;
      &lt;%= f.text_area :content, rows: 3, :class =&gt; "span8" %&gt;
      &lt;%= f.hidden_field :user_id, :value =&gt; current_user.id %&gt;
      &lt;%= f.submit "Add Comment", :class =&gt; "btn btn-header" %&gt;       
  &lt;% end %&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p><strong>view/newcomments/create.js.erb</strong></p>
  
  <pre><code>$('#newcomment').append('&lt;%= j render(@newcomments) %&gt;');
  </code></pre>
  
  <p><strong>projects_controller.rb</strong></p>
  
  <pre><code>def comments
    @commentable = @project
    @newcomments = @commentable.newcomments.newest.page(params[:comments_page]).per_page(10)
    @newcomment = Newcomment.new
  
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @project.comments }
    end
  end
  </code></pre>
  
  <p><strong>projects/comments.html.erb</strong></p>
  
  <pre><code>&lt;%= render 'comments' %&gt;
  </code></pre>
  
  <p><strong>projects/_comments.html.erb</strong></p>
  
  <pre><code>&lt;%= render @newcomments %&gt;
  </code></pre>
  
  <p><strong>view/newcomments/_newcomment.html.erb</strong></p>
  
  <pre><code>&lt;div class="comments"&gt; 
      &lt;%= link_to newcomment.user.name %&gt;&lt;/strong&gt;
      Posted &lt;%= time_ago_in_words(newcomment.created_at) %&gt; ago
      &lt;%= newcomment.content %&gt;
  &lt;/div&gt;
  
  &lt;span class="comment"&gt;
      &lt;%= form_for [@commentable, @newcomment] do |f| %&gt;
        &lt;div class="field"&gt;
          &lt;%= f.text_area :content, rows: 3, :class =&gt; "span8" %&gt;
        &lt;/div&gt;
  
        &lt;%= f.hidden_field :user_id, :value =&gt; current_user.id %&gt;
  
        &lt;div class="actions"&gt;
          &lt;%= f.submit "Add Comment", :class =&gt; "btn btn-header" %&gt;
        &lt;/div&gt;
  
      &lt;% end %&gt;
  
      &lt;% unless newcomment.newcomments.empty? %&gt;
         &lt;%= render @newcomments %&gt;
      &lt;% end %&gt;
  &lt;/span&gt;
  </code></pre>