0

I am trying to get new comments to update with ajax instead of a page reload. I followed the railscast tutorial but am getting a 500 internal server error in my js console. I was reading other posts and a lot of people are saying its a partial error but I can't figure it out. The comment will save before page reload but will not display until the page is reloaded.. Here is the comment controller

def create
  @post = Post.find(params[:post_id])
  @comment = Comment.create(params[:comment].permit(:content))
  @comment.user_id = current_user.id
  @comment.post_id = @post.id

  if @comment.save
    respond_to do |format|
      format.html {redirect_to post_path(@post.comments)}
      format.js
    end
  else
    render 'new'
  end
end

The create.js.erb file in the comment directory.

$('.comment').append('<%= j render @post.comments %>');

The comment form

<%= simple_form_for([@post, @post.comments.build], :remote => true,  :input_html => { :data => { :url => '/post/:id', :type => :js } }) do |f|   %>
  <%= f.input :content, label: "Reply"%>
  <%= f.button :submit, "Submit" %>
<% end %>
Dbz
  • 2,721
  • 4
  • 35
  • 53
nharvey27
  • 11
  • 5

2 Answers2

0

I'm not 100% sure if this is your current issue, but format.html {redirect_to post_path(@post.comments)} is trying to go to the post path for all the comments belonging to a post, which doesn't make sense. I think what you mean to do is go to the post's path.

format.html { redirect_to post_path(@post) }

If this is a partial problem, then here is a stack overflow question which will likely be helpful.

It's also possible you are making the same mistake with your partial, and you should be passing it @post instead of @post.comments. Or perhaps you should be passing it the latest comment instead of all of the comments.

EDIT:

You may need to specify the partial in your javascript:

$('.comment').append('<%= j render 'comments/form', locals: {post: @post} %>');

And in your comment form, I believe you change all the @posts to posts.

Here is another stack overflow question which may be helpful

EDIT2:

Try this out. The difference between this and my last edit is that instead of putting the form on the comments, now you put the form on the post.

# app/controllers/comments_controller.rb
def create
  @post = Post.find(params[:post_id])
  @comment = Comment.create(params[:comment].permit(:content))
  @comment.user_id = current_user.id
  @comment.post_id = @post.id

  if @comment.save
    respond_to do |format|
      format.html { redirect_to post_path(@post) }
      format.js
    end
  else
    render 'new'
  end
end

# app/views/posts/show.html.erb
# Doesn't need to look exactly like this
<div class='post'>
  <div class='content'>
    <%= @post.content %>
  </div>
  <div class='form'>
    <%=j render 'comments/form', locals: { post: @post } %>
  </div>
</div>
<div class='comment'>
  <%= render @post.comments %>
</div>

# app/views/comments/create.js.erb
$('.comment').append('<%=j render @post.comments %>');

# app/views/comments/_comment.html.erb
<%= comment.content %>

# app/views/posts/_form.html.erb
<%= simple_form_for([@post, @post.comments.build], :remote => true,  :input_html => { :data => { :url => '/post/:id', :type => :js } }) do |f|   %>
  <%= f.input :content, label: "Reply"%>
  <%= f.button :submit, "Submit" %>
<% end %>
Community
  • 1
  • 1
Dbz
  • 2,721
  • 4
  • 35
  • 53
  • Has anything changed since I've edited my post? New error messages? Have any of the solutions I provided help? – Dbz Jan 22 '16 at 01:58
  • Thanks for the post! I'm not at my home computer at the moment but i'll update you as soon as i get back. – nharvey27 Jan 22 '16 at 02:20
  • So now it will correct append a comment without page reload but now all the comments have a create comment from on them making the site look bad. Any suggestions? – nharvey27 Jan 22 '16 at 06:35
  • @nharvey27, the reason is because you are grabbing all of the comments and putting the form on them with this line: `$('.comment').append('<%= j render @post.comments %>');`. The first part `$('.comment')` will grab all of the comments, so if you don't want to put the form on all of them comments, it's important to change that. Perhaps you want to put the form on all of the posts? – Dbz Jan 22 '16 at 17:55
0
#config/routes.rb
resources :posts do
   resources :comments #-> url.com/posts/:post_id/comments/new
end

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
   respond_to :js, :html, only: :create #-> requires "responders" gem

   def create
      @post    = Post.find params[:post_id]
      @comment = @post.comments.new comment_params
      respond_with @comment
   end

   private

   def comment_params
      params.require(:comment).permit(:content).merge(user_id: current_user.id)
   end
end

#app/views/comments/create.js.erb
$('.comment').append('<%=j render @post.comments %>'); #-> requires comments/_comment.html.erb

#app/views/comments/_comment.html.erb
<%= comment.content %>

#app/views/posts/show.html.erb
<%= simple_form_for [@post, @post.comments.new], remote: true do |f| %>
   <%= f.input :content, label: "Reply"%>
   <%= f.button :submit, "Submit" %>
<% end %>

The above is how it should work.

I wrote it all out as to give the proper context etc.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147