【发布时间】:2010-01-28 02:00:18
【问题描述】:
我目前正在使用 Ruby on Rails 实现一个类似博客的网站。每个“帖子”都有一些 cmets。目前我只有最新的 5 个 cmets 加载,但我想实现一个超链接来加载其余的 cmets。不幸的是,在我目前的实现中,我得到的只是一堆垃圾。
之前
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
点击“查看更多cmets”后
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 有:
<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
最后是我的 morecmets.js.rjs:
@comments.each do |p|
page.insert_html :bottom, :comments, :partial => p
end
page.hide 'morecomments'
我真的是 Rails 的新手,我真的不知道所有的元魔法 Rails 在后台做什么。任何帮助都会很棒。
谢谢!
【问题讨论】:
标签: ruby-on-rails ajax