title: Add comments + Ajax
body: <p>I've started learning rails and I would like to add ajax to comments</p>
  
  <p>Comments should be added using Ajax</p>
  
  <p>Can you please help or link to an example?</p>
  
  <p>Here if my config.rb: </p>
  
  <pre><code>  resources :posts do
      resources :comments
    end
  </code></pre>
  
  <p>in post/show.html.erb </p>
  
  <pre><code>&lt;p&gt;&lt;%= @post.title %&gt;&lt;/p&gt;
  
      &lt;h2&gt;Comments:&lt;/h2&gt;
      &lt;div id='com'&gt;
        &lt;%= render @post.comments %&gt;
      &lt;/div&gt;
  
  &lt;h2&gt;Add a comment:&lt;/h2&gt;
  &lt;%= render "comments/form" %&gt;
  </code></pre>
  
  <p>in view/comments/_comment.html.erb:</p>
  
  <pre><code>&lt;p&gt;
    &lt;strong&gt;Commenter:&lt;/strong&gt;
    &lt;%= comment.commenter %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;strong&gt;Comment:&lt;/strong&gt;
    &lt;%= comment.body %&gt;
  &lt;/p&gt;
  &lt;%= link_to "Del", [comment.post, comment], :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;
  &lt;hr&gt;
  </code></pre>
  
  <p>in comments/form.html.erb</p>
  
  <pre><code>&lt;%= form_for([@post, @post.comments.build], remote: true) do |f| %&gt;
  .
  .
  .
  </code></pre>
  
  <p>in comments_controller:</p>
  
  <pre><code>class CommentsController &lt; ApplicationController
  
    def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(params[:comment])
  
      respond_to do |format|
          format.html { redirect_to post_path(@post) }
          format.js 
      end
    end
  </code></pre>
  
  <p>and in view add file : _create.js.erb</p>
  
  <pre><code>$('#com').html("&lt;%=j render @post.comments %&gt;");
  </code></pre>
  
  <p>without remote true
  all works ( but reload page ) 
  action delete works to </p>
  
  <p>in application.html.erb</p>
  
  <pre><code>  &lt;%= javascript_include_tag 'application' %&gt;
  </code></pre>
  
  <p>when i click to submit form - nothing happens</p>
  
  <p>but when i (after submit) reload page - i saw added comment(s)</p>