如何在同一页面上显示索引和实例项?
我有一堆帖子。
- 我希望对于索引操作,帖子列表应显示在侧边栏中,并在右侧显示一些随机文本。
- 对于显示特定帖子的操作,帖子列表仍应显示在侧边栏中,帖子本身应显示在右侧。 (请参阅附图了解更多详细信息)
在 Rails 中执行此操作的习惯用法是什么?什么是“正确”的方法?
更新
@coreyward,谢谢:
所以,我会:
- _sidebar_list.html.erb (用于帖子列表)
- index.html.erb =>渲染侧边栏和随机文本
- show.html.erb =>渲染侧边栏和帖子
但是然后,布局(左侧栏,右侧内容)(图像中的黑框) 需要在索引和显示模板中重复。 解决这个问题的方法是制作另一个部分(包含左侧栏,右侧内容) 然后在索引中渲染该部分并显示?
I have a bunch of Posts.
- I want that for the index action, the list of posts should be displayed in a sidebar and some random text in the right side.
- For a action to display a specific post, the list of posts should still be displayed in a sidebar and the post itself on the right side.
(Please see attached image for more details)
What is the idiom to do this in Rails? What's the "right" way?
UPDATE
@coreyward, thanks:
So, I would have:
- _sidebar_list.html.erb (for list of posts)
- index.html.erb => render sidebar and random text
- show.html.erb => render sidebar and post
But then, the layout (left side bar, right content) (the black box in image)
would need to be repeated in index and show templates.
Is the solution to this making yet another partial (containing left side bar, right content)
and then rendering that partial in index and show?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您知道如何编写 HTML,并且您已经知道您的“随机内容”。也就是说,
index
操作应该非常简单:获取帖子集合并在侧边栏中呈现指向它们的链接。将它们放在像“_sidebar_list.html.erb”这样的部分中并从那里渲染它是有意义的:对于您的
show
操作,您需要获取相同的帖子集合进行显示在控制器的侧边栏中...例如:然后,除了显示当前帖子的数据之外,您还可以在显示视图中呈现侧边栏列表。
希望这可以帮助您朝着正确的方向开始。如果您有任何不清楚的地方,请随时发表评论。
I'm assuming you know how to do the HTML, and that you already know your "random content". That said, the
index
action should be pretty straightforward: grab the collection of posts and render links to them in the sidebar there. It would make sense to put these in a partial like "_sidebar_list.html.erb" and rendering it from there like so:For your
show
action, you would want to grab the same collection of posts for display in the sidebar in your controller…such as:Then you would render the sidebar list in your show view, in addition to displaying the data for the current post.
Hope that helps you get started in the right direction. Feel free to comment if you're unclear on anything.