Ruby on rails 3 在Rails 3中使用Ajax时,是否需要重新加载注释对象或包含注释的部分?
在阅读了几篇不同的教程之后,我一直在尝试在不使用Ajax重新加载页面的情况下添加注释,到目前为止,这是我得出的结论,但它不起作用: 内部用户\u comments/\u comments.html.erbRuby on rails 3 在Rails 3中使用Ajax时,是否需要重新加载注释对象或包含注释的部分?,ruby-on-rails-3,jquery,comments,remotipart,Ruby On Rails 3,Jquery,Comments,Remotipart,在阅读了几篇不同的教程之后,我一直在尝试在不使用Ajax重新加载页面的情况下添加注释,到目前为止,这是我得出的结论,但它不起作用: 内部用户\u comments/\u comments.html.erb <div id="comment_form"> <%= simple_form_for [@commentable, @comment], :html => { :multipart => true }, :remote => true do |
<div id="comment_form">
<%= simple_form_for [@commentable, @comment], :html => { :multipart => true }, :remote => true do |f| %>
<div class="picture"><%= image_tag current_user.avatar.url(:thumb) %></div>
<%= f.input :content, label: false, :placeholder => "Add Comment", :input_html => { :rows => 4 } %>
<%= f.submit "Add Comment" %>
<% end %>
</div>
在create.js.erb
// Display a Javascript alert
<% if remotipart_submitted? %>
$("#comments_list").append("<%= escape_javascript(render(:partial => 'user_comments/comments')) %>");
<% end %>
这意味着帖子会通过,但评论不会被添加回部分内容。两天后就可以了!我修复了它,以下是我可以分享并可能有所帮助的内容: 1-确保将
:remote=>true
包含到即将提交的表单中
2-检查控制器并查看创建操作被重定向的内容,在我的情况下,我更改为:
def create
@users = User.all
@comment = @commentable.user_comments.new(params[:user_comment])
@comment.user_id = current_user[:id]
#@commentable.user_comments.create(:user_id => current_user[:id])
if @comment.save
flash[:notice] = "Successfully created comment."
respond_to do |format|
format.html
format.js {@comments = @commentable.user_comments}
end
else
render :new
end
end
然后确保正确编写create.js.erb
:
$("#comments_list").empty()
$("#comments_list").append("<%= escape_javascript(render(:partial => 'comments')) %>");
$(“#注释列表”).empty()
$(“#评论列表”)。追加(“‘评论”))%>”;
好了!我希望有些人能为像我这样的新手创建一个合适的教程:)
def create
@users = User.all
@comment = @commentable.user_comments.new(params[:user_comment])
@comment.user_id = current_user[:id]
#@commentable.user_comments.create(:user_id => current_user[:id])
if @comment.save
flash[:notice] = "Successfully created comment."
respond_to do |format|
format.html
format.js {@comments = @commentable.user_comments}
end
else
render :new
end
end
$("#comments_list").empty()
$("#comments_list").append("<%= escape_javascript(render(:partial => 'comments')) %>");