doyz
doyz

Reputation: 886

Rails 5 + Ajax: Comment appended only if a comment exists

I am trying to create a feature where users can create comments on articles with Ajax. However, i can't understand why the comment can only be successfully rendered through ajax if one comment exists. The comment is committed to database without any rollback.

If i reload the page and create a second comment, only then will the new comment be appended.

Started POST "/articles/veniam-ipsum-eos-quas-aut-rerum-consequatur-at-velit-perferendis-odio/comments" for 103.252.202.198 at 2017-11-03 16:50:14 +0000
Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "comment"=>{"content"=>"comment only appended if a comment exists"}, "commit"=>"Add Comment", "article_id"=>"veniam-ipsum-eos-quas-aut-rerum-consequatur-at-velit-perferendis-odio"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Article Load (0.6ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2  [["slug", "veniam-ipsum-eos-quas-aut-rerum-consequatur-at-velit-perferendis-odio"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  SQL (0.6ms)  INSERT INTO "comments" ("content", "article_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["content", "comment only appended if a comment exists"], ["article_id", 189], ["user_id", 1], ["created_at", "2017-11-03 16:50:14.291049"], ["updated_at", "2017-11-03 16:50:14.291049"]]
   (2.5ms)  COMMIT

Comments controller

class CommentsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_article

  def create
    @comment = @article.comments.build(comment_params)
    @comment.user = current_user
      if @comment.save
        respond_to do |format|
          format.html do 
            flash[:success] = "Your comment was created successfully"
            redirect_to @comment.article
          end
            format.js
        end
        unless @comment.article.user == current_user
          Notification.create!(recipient: @article.user, actor: current_user, action: "posted", notifiable: @comment)
        end
      else
       respond_to do |format|
          format.html { redirect_to @comment.article, flash[:error] = "Unable to submit comment."}
        end
      end
  end

  private

    def set_article
      @article = Article.friendly.find(params[:article_id])
    end

    def comment_params
      params.require(:comment).permit(:content)
    end

end

Articles controller

  def show
      @comments = @article.comments.order("created_at DESC")
      @new_comment = @article.comments.new
  end

articles/show.html.erb

  <%= render 'comments/comment_form' %>
  <% if @comments.exists? %>
    <div id= "comment" >
      <%= render @comments %>
    </div>
  <% else %>
    <div class ="no-comments">
      <p> There are no comments yet.</p>
    </div>
  <% end %>

comments/_comment_form.html.erb

  <%= form_for [@article, @new_comment], remote: true do |f| %>
    <div class="form-group">
      <div class = "row">
        <div class= "col-md-9 col-sm-9 col-xs-12">
          <%= f.text_area :content, rows: 2, placeholder: "Write your comment...", class: 'form-control' %>
        </div>
        <div class= "col-md-3 col-sm-3 col-xs-12">
          <%= f.submit 'Add Comment', class: 'btn btn-md btn-default' %>
        </div>
      </div>
    </div>
  <% end %>

comments/_comment.html.erb

<%= comment.content %>

comments/create.js.erb

$('#comment').append("<%= escape_javascript (render partial: @comment) %>");

routes:

  resources :articles do
    resources :comments
  end

SOLUTION:

User PlanB gave a working solution below, but i tweaked the answer a little so that <p> There are no comments yet.</p> is removed after the comment form is submitted.

articles/show.html.erb

  <div id="comment-form">
    <%= render 'comments/comment_form' %>
  </div>
  <div id = "comment-list" >
    <% if @comments.exists? %>
        <%= render @comments %>
    <% else %>
        <div id = "no-comments">
          <p> There are no comments yet.</p>
        </div>
    <% end %>
  </div>

comments/create.js.erb

$('#comment-list').append("<%= escape_javascript (render partial: @comment) %>");
$('#no-comments p').html('');

Upvotes: 2

Views: 134

Answers (2)

Chivorn Kouch
Chivorn Kouch

Reputation: 351

I believe the problem is in your create method. Since you have redirected to @comment.article, create.js.erb cannot be reached anymore. Try to rewrite the create method like this:

def create
  @comment = @article.comments.build(comment_params)
  @comment.user = current_user
  if @comment.save
    respond_to do |format|
      format.html do 
        flash[:success] = "Your comment was created successfully"
      end
      format.js
    end
    unless @comment.article.user == current_user
      Notification.create!(recipient: @article.user, actor: current_user, action: "posted", notifiable: @comment)
    end
  else
    respond_to do |format|
      format.html { redirect_to @comment.article, flash[:error] = "Unable to submit comment."}
    end
  end
end

Upvotes: 0

Julius Dzidzevičius
Julius Dzidzevičius

Reputation: 11000

Try something like:

  <%= render 'comments/comment_form' %>
  <% if @comments.exists? %>
    <div class= "comment" >
      <%= render @comments %>
    </div>
  <% else %>
    <div class= "comment">
      <p> There are no comments yet.</p>
    </div>
  <% end %>

and:

$('.comment').append("<%= escape_javascript (render partial: @comment) %>");

Because your if statement doesn't create div#comment if there are now comments, so

$('#comment').append("<%= escape_javascript (render partial: @comment) %>"); cant append anything to #comment since it doesn't exist in HTML.

Upvotes: 1

Related Questions