2016-01-22 88 views
0

我想获得新的评论用ajax而不是页面重新加载更新。我遵循railscast教程,但在我的js控制台中出现500内部服务器错误。我正在阅读其他帖子,很多人都说这是一个部分错误,但我无法弄清楚。注释将重新加载页面之前保存,但不会显示,直到重新加载页面。这里是注释控制器Rails的Ajax 500服务器错误

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 

在注释目录中的文件create.js.erb。

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

评论表单

<%= 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 %> 
+0

什么是评论形式命名? – Dbz

+0

Just _form.html.erb – nharvey27

+0

有关此状态的任何更新? – Dbz

回答

0

我不是100%肯定,如果这是你目前的问题,但format.html {redirect_to post_path(@post.comments)}试图去post路径都属于文章的评论,这是没有意义的。我认为你的意思是去post的道路。

format.html { redirect_to post_path(@post) }

如果一个局部问题,那么here is a stack overflow question这将可能是有益的。

也有可能你对你的部分犯了同样的错误,你应该通过@post而不是@post.comments。或者,也许你应该通过最新的评论,而不是所有的评论。

编辑:

您可能需要指定在JavaScript部分:

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

而且在您的评论的形式,我相信你改变所有的@post s到post秒。

这里是another stack overflow question which may be helpful

EDIT2:

尝试了这一点。这与我最后一次编辑的区别在于,不是将表单放在评论上,而是现在将表单放在帖子上。

# 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 %> 
+0

自编辑我的文章以来有什么改变吗?新的错误消息?有任何我提供的解决方案的帮助? – Dbz

+0

感谢帖子!我目前不在家里的电脑上,但我一回来就会更新你的电话。 – nharvey27

+0

所以现在它会更正附加评论没有页面重新加载,但现在所有的评论有一个创建评论,从他们使网站看起来不好。有什么建议么? – nharvey27

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 %> 

以上是它应该如何工作。

我全都写出来给出正确的上下文等。

+0

如果整个部分是'<%= comment.content%>',那么是否真的需要部分评论? – Dbz

+0

这是他是否调用'<%= @渲染%post.comments>':) –

+0

我改成了你如何指定的,我现在得到一个例外:未定义的方法'comments_url”为# nharvey27