简体   繁体   English

如果在Rails 3中使用Ajax,是否需要重新加载注释对象或包含注释的部分内容?

[英]Do I need to reload the comment object or the partial that contains the comment in case of using Ajax in Rails 3?

I have been trying to figure out adding comments without reloading the page using Ajax, after reading few different tutorials this is what I came up to so far, and it's not working: 在阅读了几本不同的教程之后,我一直试图弄清楚添加注释而不用Ajax重新加载页面,这是我到目前为止所做的,并且不起作用:

inside user_comments/_comments.html.erb 在user_comments / _comments.html.erb内部

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

Inside the controller: 控制器内部:

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 { redirect_to @commentable }
    format.js
    #format.js #{ render 'create.js.erb' }
    end
else
  render :new
end
 end

and inside the create.js.erb 并在create.js.erb中

// Display a Javascript alert
<% if remotipart_submitted? %>
    $("#comments_list").append("<%= escape_javascript(render(:partial => 'user_comments/comments')) %>");
<% end %>

I'm using a Gem called: remotipart 我使用的宝石是: remotipart

I don't know what I'm missing in the process. 我不知道我在此过程中缺少什么。 in the console I get: 在控制台中,我得到:

POST http://localhost:3000/assignments/2/user_comments

200 OK
        134ms

which means the post goes through, but the comment doesnt get added back to the partial. 这意味着帖子可以通过,但评论不会添加回部分。

Ok after 2 days! 2天后还可以! I fixed it, here is what I can share and might help: 我已修复它,这是我可以分享的内容,可能会有所帮助:

1- make sure to include the :remote => true to the form that is about to be submitted 2- Check the controller and see what the Create action is being redirected, in my case I changed to this: 1-确保将:remote => true包含在将要提交的表单中2-检查控制器,并查看正在重定向的Create操作,在我的情况下,我更改为:

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

Then make sure the create.js.erb is written properly: 然后确保正确编写了create.js.erb

    $("#comments_list").empty()
$("#comments_list").append("<%= escape_javascript(render(:partial => 'comments')) %>");

there you go! 你去! I hope some creates a proper tutorial for newbies like me :)! 我希望有人能为像我这样的新手创建适当的教程:)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM