Javascript Rails Ajax注释创建工作不正常
我正在尝试使用ajax请求创建注释。实际的问题是注释是否正确提交,响应是否为create.js.erb也会呈现,但在创建后注释不会刷新Javascript Rails Ajax注释创建工作不正常,javascript,jquery,ruby-on-rails,ruby,ajax,Javascript,Jquery,Ruby On Rails,Ruby,Ajax,我正在尝试使用ajax请求创建注释。实际的问题是注释是否正确提交,响应是否为create.js.erb也会呈现,但在创建后注释不会刷新 comments_controller.rb class CommentsController < ApplicationController def create @comment = Comment.new(comments_params.except(:post_id)) @post_id = params[:comment
comments_controller.rb
class CommentsController < ApplicationController
def create
@comment = Comment.new(comments_params.except(:post_id))
@post_id = params[:comment][:post_id].to_i
@comments = Post.find(@post_id).comments.page(params[:page])
@comment.user_id = 1 # to be replaced with current_user
if @comment.save
flash[:notice] = "Commented"
else
flash[:alert] = "something went wrong"
end
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
private
def comments_params
params.require(:comment).permit(:body, :commentable_id, :commentable_type, :post_id)
end
end
posts/show.html.erb的一部分
<div class="panel-body">
<div id="comments">
<%= render partial: "comments/comment", collection: @comments, locals: { post_id: @post.id } %>
</div>
<div class="row">
<div class="col-md-offset-4 col-md-4" id="paginator"><%= paginate @comments %></div>
</div>
<div>
<%= simple_form_for(@post.comments.new, remote: true) do |f| %>
<%= f.input :body, label: "New Comment" %>
<%= f.input :commentable_id, as: :hidden, value: @post.id %>
<%= f.input :commentable_type, as: :hidden, value: "Post" %>
<%= f.input_field :post_id, as: :hidden, value: @post.id %>
<%= f.button :submit, class: "submit-btn" %>
<% end %>
</div>
</div>
_comment.html.erb
<div class="comment-section">
<div class="comment">
<p><%= markdown(comment.body) =%></p>
<small><%= time_ago_in_words(comment.created_at) %> ago</small>
<small class="comment-reply"><a href="#">Reply</a></small>
<small class="comment-user">By: <%= "Dummy user" %></small><!-- comment.user.name -->
</div>
<div class="comment2">
<%= render partial: "comments/comment", collection: comment.comments, locals: { post_id: post_id } if comment.comments.any? %>
</div>
<div class="comment-form">
<%= simple_form_for(comment.comments.new, remote: true) do |f| %>
<%= f.input :body, label: "Reply" %>
<%= f.input :commentable_id, as: :hidden, value: comment.id %>
<%= f.input :commentable_type, as: :hidden, value: "Comment" %>
<%= f.input_field :post_id, as: :hidden, value: post_id %>
<%= f.button :submit, class: "submit-btn" %>
<% end %>
</div>
</div>
以前
作者:
注释/create.js.erb
#("#comments").html("<%= escape_javascript render partial: 'comments/comment', collection: @comments, locals: { post_id: @post_id } %>");
#("#paginator").html("<%= escape_javascript paginate(@comments, remote: true).to_s %>");
#(“#注释”).html(“”);
#(“#paginator”).html(“”);
我不确定这是否是破坏性问题,但您有两个id=“comments”的div,一个在show中,一个在comment中。我不确定jQuery在期望只找到一个项而只找到两个项时如何响应,但这可能就是错误所在。您是否尝试过通过抓取ID来替换javascript控制台中的html?为什么您的partial再次调用同一个partial?我有一个comment属于comment,所以递归调用partial可以完成此任务
#("#comments").html("<%= escape_javascript render partial: 'comments/comment', collection: @comments, locals: { post_id: @post_id } %>");
#("#paginator").html("<%= escape_javascript paginate(@comments, remote: true).to_s %>");