目前,我在模板中有一个显示注释的每个循环。假设用户在他们发布的微博上有50条评论。将显示一个显示50个评论的长列表。
为了节省页面上的空间,我决定将显示的评论限制在每个微博上2-3条。如果用户希望查看更多内容,可以单击“查看更多”或“查看全部”。我想知道,如果有大约10,000个注释,用户单击“全部视图”,服务器将如何处理,这就是为什么我可能选择实现“查看更多”,然后显示更多的评论。
无论如何,我想知道一个很好的方法来限制显示给用户的评论数量,直到他们选择查看所有的?。
如果我走jquery/js路线,只显示最近的2-3条消息,其他消息就会被加载到后端,难道不是一个更好的选择,在rails中控制它吗?
我真的很想要一些好的解决方案/信息,关于最好的方法来做这件事。
如果你需要更多的信息,我很乐意提供。
谢谢亲切的问候
发布于 2012-04-16 12:36:07
你可以像Facebook一样:
在Facebook上,你不能一次加载超过50条评论。我觉得你也该这么做。
发布于 2012-04-16 12:55:03
干净的方法是实现对评论的分页。
发布于 2012-04-16 13:02:42
我认为在belongs_to
和Comment
之间有一个简单的Post
和Comment
关系。我通常会:
路线:
resources :posts do
resources :comments
end
模型:设置默认页面大小:
class Comments < ActiveRecord::Base
belongs_to :post
DEFAULT_PAGE_SIZE = 25
end
主计长:
class CommentsController
def index
post = Post.find(params[:post_id])
offset = params[:offset] || 0
limit = params[:limit] || Comment::DEFAULT_PAGE_SIZE
@comments = post.comments.offset(offset).limit(limit)
respond_to do |format|
#respond as you like
end
end
# more actions...
end
视图中,加载更多的链接,例如,通过ajax加载注释:
<%= link_to "load more comments", post_comments_path(@post, :format => 'js'), :method => :get, :remote=>true id='load-more-comments' %>
您还希望将偏移量绑定到ajax post:
$ ->
$('#load-more-comments').on 'ajax:before', (event) ->
el = $(this)
offset = #count your offset, I often do by counting the <li>s already in the <ul>
el.data 'params', "offset=#{offset}"
# you could also pass the limit: el.data 'params', "offset=#{offset}&limit=#{some limit}"
.on 'ajax:complete', (event, xhr, status) ->
el = $(this)
el.removeData 'params' # remember to remove this.
我也对做这件事的更好的方法感兴趣。期待着答案和批评。:)
https://stackoverflow.com/questions/10174127
复制相似问题