如何清理我的视图
我正在尝试创建一篇博客文章的评论部分,其中评论列在帖子下方。
我有 Posts
和 PostComments
类
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想在它上面调用 will_paginate
最好在你的控制器中定义这个返回的实例属性
在你的视图中
在这种情况下,这个循环只是记录真正的视图。不是所有记录。
If you want call will_paginate on it do
And it's better to define in your controller are instance attribute to this return
And in you view
In this case, this loop is only to record really view. Not all records.
尝试渲染部分集合:
这将渲染
/post_comments/_post_comment.erb
并将局部变量post_comment
传递给模板进行显示。迭代计数器将自动提供给模板,其名称格式为 post_comment_counter。如果您有可用的页面、页面大小参数,那么您可以直接在视图中调用分页。
Try the render partial collection :
This will render
/post_comments/_post_comment.erb
and pass the local variablepost_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.