如何清理我的视图

发布于 2024-09-30 11:31:31 字数 549 浏览 4 评论 0原文

我正在尝试创建一篇博客文章的评论部分,其中评论列在帖子下方。

我有 PostsPostComments

我有 posts/show.html.erb 来显示博客文章,并且我做了一个 post_comments /_post_comment.html.erb 部分在 posts/show.html.erb 中呈现评论

我有以下内容:

<% @post.post_comments.each do |comment| %> 
    <%= render :partial => '/post_comments/post_comment', :locals=>{ :comment => comment } %>
<% end %>

有没有办法将该循环移出视图并放入控制器中的方法?我想对其调用 will_paginate,如果逻辑像现在一样在视图中,我认为我无法做到这一点。

I'm trying to do a blog post's comments section where comments are listed below the post.

I have Posts and PostComments classes

I have posts/show.html.erb to show the blog post and I have made a post_comments/_post_comment.html.erb partial to render a comment

in posts/show.html.erb i have the following:

<% @post.post_comments.each do |comment| %> 
    <%= render :partial => '/post_comments/post_comment', :locals=>{ :comment => comment } %>
<% end %>

Is there any way to move that loop out of the view and into a method in the controller? I want to call will_paginate on it, and I don't think I can do that if the logic is in the view like it is now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖个坑埋了你 2024-10-07 11:31:31

如果你想在它上面调用 will_paginate

<% @post.post_comments.paginate(params[:page], params[:per_page]).each do |comment| %> 
    <%= render :partial => '/post_comments/post_comment', :locals=>{ :comment => comment } %>
<% end %>

最好在你的控制器中定义这个返回的实例属性

@post_comments = @post.post_comments.paginate(params[:page], params[:per_page])

在你的视图中

<% @post_comments.each do |comment| %> 
    <%= render :partial => '/post_comments/post_comment', :locals=>{ :comment => comment } %>
<% end %>
<%= will_paginate(@post_comments) %>

在这种情况下,这个循环只是记录真正的视图。不是所有记录。

If you want call will_paginate on it do

<% @post.post_comments.paginate(params[:page], params[:per_page]).each do |comment| %> 
    <%= render :partial => '/post_comments/post_comment', :locals=>{ :comment => comment } %>
<% end %>

And it's better to define in your controller are instance attribute to this return

@post_comments = @post.post_comments.paginate(params[:page], params[:per_page])

And in you view

<% @post_comments.each do |comment| %> 
    <%= render :partial => '/post_comments/post_comment', :locals=>{ :comment => comment } %>
<% end %>
<%= will_paginate(@post_comments) %>

In this case, this loop is only to record really view. Not all records.

苏璃陌 2024-10-07 11:31:31

尝试渲染部分集合:

<%= render :partial    => '/post_comments/post_comment', 
           :collection => @post_comments  %>

这将渲染 /post_comments/_post_comment.erb 并将局部变量 post_comment 传递给模板进行显示。迭代计数器将自动提供给模板,其名称格式为 post_comment_counter。

如果您有可用的页面、页面大小参数,那么您可以直接在视图中调用分页。

<%= render :partial    => '/post_comments/post_comment', 
           :collection => @post.post_comments.paginate(:page => params[:page]) %>

Try the render partial collection :

<%= render :partial    => '/post_comments/post_comment', 
           :collection => @post_comments  %>

This will render /post_comments/_post_comment.erb and pass the local variable post_comment to the template for display. An iteration counter will automatically be made available to the template with a name of the form post_comment_counter.

If you have the page, page size parameters available then you can call paginate directly in the view.

<%= render :partial    => '/post_comments/post_comment', 
           :collection => @post.post_comments.paginate(:page => params[:page]) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文