如何在另一个控制器的视图内显示视图?
我有 2 个控制器:文章和评论。对于每篇文章,我都需要显示评论。
它们各自都工作得很好,但是我怎样才能在控制器/节目中获取评论/索引而不弄乱一切?
I have 2 controllers: articles and comments. For each articles I need to display comments.
Both of them work fine on their own, but how can I get the comments/index inside the controller/show without messing everything up ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的显示视图中,您将执行类似的操作(假设您有一个 app/view/comments 视图文件夹和一个 _comment.html.erb ):
In your show view you would do something like this (assuming you have a app/view/comments view folder and a _comment.html.erb inside):
您可以将每个评论呈现为部分评论。因此,在您的评论索引页面中,仅呈现所有评论的部分内容。
然后,从您的文章显示页面,呈现与该文章相关的评论的部分评论。
看一下在 Rails 中使用 partials。
You can make each comment render as a partial. So, from your comments index page, just render partial on all of the comments.
Then, from your articles show page, render the comments partial for the comments related to that article.
Take a look at using partials in Rails.