Ruby on rails Rails中的AJAX。无法在帖子上创建评论

Ruby on rails Rails中的AJAX。无法在帖子上创建评论,ruby-on-rails,ajax,forms,Ruby On Rails,Ajax,Forms,我试图允许用户使用Rails中的AJAX在特定帖子上创建评论。我得到了要显示的表单,但在创建时它不起作用。在我的表格中,它表示href=“/posts/post\u id/comments/new”method=“post”。我认为它应该是post/post\u id/comments with a method=“post”,但我无法实现这一点 控制台只是说没有路由匹配POST/id/comments/new,因为这显然是错误的 注释\u controller.rb class Comment

我试图允许用户使用Rails中的AJAX在特定帖子上创建评论。我得到了要显示的表单,但在创建时它不起作用。在我的表格中,它表示href=“/posts/post\u id/comments/new”method=“post”。我认为它应该是post/post\u id/comments with a method=“post”,但我无法实现这一点

控制台只是说没有路由匹配POST/id/comments/new,因为这显然是错误的

注释\u controller.rb

class CommentsController < ApplicationController

respond_to :html, :js

def index 
    @comments = Comment.all
end 

def new
    @new_comment = Comment.new
end

def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
end
class CommentsController
_评论视图中的form.html.erb

<%= simple_form_for post_comments_path, remote: true, html:{class: 'form-inline'} do |f| %>
  <%= f.input :body, input_html: { class: 'form-control' } %>
  <%= f.button :submit, :class => "btn btn-success", value: "Post Comment" %>
<% end %>

“btn btn成功”,值:“发表评论”%>
index.html.erb(带有帖子的主视图)


+评论
0 %>
评论中的new.js.erb

$('#comment-form').html("<%= j (render 'form') %>");
$('#comment-form').slideDown(350);
$('#comments').html("<%= j (render @comments) %>");
$('#comment-form').slideUp(350);
$('#注释表单').html(“”);
$(“#评论表”)。向下滑动(350);
在注释中创建.js.erb

$('#comment-form').html("<%= j (render 'form') %>");
$('#comment-form').slideDown(350);
$('#comments').html("<%= j (render @comments) %>");
$('#comment-form').slideUp(350);
$('#comments').html(“”);
$(“#评论表”)。slideUp(350);

您不应该将路径帮助程序传递给帮助程序的表单。给它一个模型,它将自己生成正确的url。试试这个:

<%= simple_form_for @new_comment, remote: true, html:{class: 'form-inline'} do |f| %>
  <%= f.input :body, input_html: { class: 'form-control' } %>
  <%= f.button :submit, :class => "btn btn-success", value: "Post Comment" %>
<% end %>
然后将@post和@new_注释都传递给表单。同样,这应该足以让Rails知道在哪里发布表单

<%= simple_form_for [@post, @new_comment], remote: true, html:{class: 'form-inline'} do |f| %>
  <%= f.input :body, input_html: { class: 'form-control' } %>
  <%= f.button :submit, :class => "btn btn-success", value: "Post Comment" %>
<% end %>

“btn btn成功”,值:“发表评论”%>
更新: create.js.erb中有一个输入错误,@comments没有定义,应该是单数:@comment 还要确保您确实有_commentpartial:)

$('#comments').html(“”);
$(“#评论表”)。slideUp(350);

由于您试图通过表单将评论绑定到帖子,因此需要将
@post
传递到路径中。所以它看起来像:

阅读以下评论后更新了

<%= simple_form_for [@post, @new_comment], remote: true, html:{class: 'form-inline'} do |f| %>
  <%= f.input :body, input_html: { class: 'form-control' } %>
  <%= f.button :submit, :class => "btn btn-success", value: "Post Comment" %>
<% end %>

“btn btn成功”,值:“发表评论”%>

注意
@post
?这就是路径渲染不正确的原因。

现在post路由的路由是正确的。谢谢你,但我有500个错误。我认为这是js文件中的render@comments.ActionView::Template::Error('nil'不是与ActiveModel兼容的对象。它必须实现:to_partial_path。):1:$('#comments').html(“”);2:$('评论表')。slideUp(350);app/views/comments/create.js.erb:1:在“app\u views\u comments\u create\u js\u erb\u 5670742253688858152\u 70133586532740”中
<%= simple_form_for [@post, @new_comment], remote: true, html:{class: 'form-inline'} do |f| %>
  <%= f.input :body, input_html: { class: 'form-control' } %>
  <%= f.button :submit, :class => "btn btn-success", value: "Post Comment" %>
<% end %>