加载使用AJAX Rails中更多评论更多评论、加载、AJAX、Rails

2023-09-10 17:45:16 作者:有气质的

我目前正在实施使用Ruby on Rails的一个类似博客的网站。每个邮报有一些意见。目前,我只有最新的5条评论负载,但我想要实现将加载的意见,其余的超链接。不幸的是我目前的执行情况,我得到的是一堆垃圾吐了出来。

I am currently implementing a blog-like site using Ruby on Rails. Each "Post" has some comments. Currently I only have the latest 5 comments load, but I want to implement a hyperlink that would load the rest of the comments. Unfortunately with my current implementation, all I get is a bunch of junk spit out.

Comments

Posted 7 minutes ago 
New

Posted 1 day ago 
Comment 3

Posted 1 day ago 
Comment 2

Posted 1 day ago 
Comment 1

Posted 1 day ago 
This is a new comment

View more comments

之后点击查看更多评论

Comments

try { Element.insert("comments", { bottom: "
\n
\n Posted 1 day ago\n 
\n Comment 2\n

\n
" }); Element.insert("comments", { bottom: "
\n
\n Posted 1 day ago\n 
\n Comment 3\n

\n
" }); Element.insert("comments", { bottom: "
\n
\n Posted less than a minute ago\n 
\n New\n

\n
" }); Element.hide("morecomments"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.insert(\"comments\", { bottom: \"
\\n
\\n Posted 1 day ago\\n 
\\n Comment 2\\n

\\n
\" });\nElement.insert(\"comments\", { bottom: \"
\\n
\\n Posted 1 day ago\\n 
\\n Comment 3\\n

\\n
\" });\nElement.insert(\"comments\", { bottom: \"
\\n
\\n Posted less than a minute ago\\n 
\\n New\\n

\\n
\" });\nElement.hide(\"morecomments\");'); throw e }

帖子的show.html.erb有:

The post's show.html.erb has:

<div id="morecomments">
  <%= link_to_remote "View more comments", :url=>{:action=>'morecomments',:post_id=>@post.id},:update=>'comments' %>
</div>

在我的岗位控制器我有

def morecomments
  @post = Post.find(params[:post_id])
  @comments = @post.comments.values_at(Range.new(5, @post.comments.size-1))
  respond_to do |format|
    format.html {redirect_to @post.comments}
    format.js
  end
end

最后我morecomments.js.rjs:

And finally my morecomments.js.rjs:

@comments.each do |p|
  page.insert_html :bottom, :comments, :partial => p
end
page.hide 'morecomments'

我真的很新的Rails,我真的不知道有关的元魔轨一切都在后台做。任何帮助将是真棒。

I'm really new to Rails, and I don't really know about all of the meta magic rails is doing in the background. Any help would be awesome.

感谢您!

推荐答案

您的问题是,你混的概念。你得到你所选择使用 link_to_remote 用的:更新选项,或使用RJS。不能同时使用。

Your problem is that you're mixing concepts. You get your choice of using link_to_remote with the :update option, or using RJS. Not both.

在使用 link_to_remote 的:你的观点希望接收HTML的一大块将​​取代匹配的ID的DOM元素的内部HTML提供的更新选项:更新。在你的情况,这是评论专区

When you use link_to_remote with the :update option your view expects to receive a chunk of html which will replace the inner html of the DOM element matching the id provided to :update. In your case, this is the comments div.

您格式.js文件部分渲染RJS模板,因为你还没有告诉它做任何事情,它的产生的JavaScript,其中link_to_remote将其视作HTML使用它来代替评论专区

Your format .js section is rendering the rjs template because you haven't told it to do anything else and it's generating javascript, which the link_to_remote treats as html uses it to replace the comments div.

您有两种解决方法:

使用 link_to_remote :更新

您的观点并没有改变,你的RJS文件闲置无用。为了实现这个解决这一替换控制器的foramt.js行:

Your view doesn't change, and your rjs file goes unused. To implement this fix replace the foramt.js line in your controller with this:

 format.js { render :partial => 'comments', :collection => @comments}

使用RJS

ajax无刷新评论的思路,ajax无刷新评论功能

Use RJS

在该溶液中,控制器不改变。所有你需要做的就是删除:更新=&GT;意见 link_to_remote 通话

In this solution, your controller doesn't change. all you have to do is remove the , :update =>'comments' from your link_to_remote call.