复合应用的最佳实践
我是 Rails 新手,我想知道创建复合视图的最佳实践是什么。
我的模型有一个显示视图,假设它称为“项目”。在我的项目显示视图中,我必须显示角色列表。
角色和项目彼此之间并不真正相关,但要求将它们显示在同一页面上。
我可以做这样的事情:
def show
@project = Project.first
@characters = Character.all
end
并有一个部分列出我的角色。 这是最好的方法吗?
I am new to rails and I would like to know which is the best practice for creating composite views.
I have a show view for my model, let's say it is called Project. In my project show view, I have to show a list of Characters.
Characters and Project are not really related to each other but it is a requirement to show them on the same page.
I could do something like this:
def show
@project = Project.first
@characters = Character.all
end
And have a partial for listing my characters.
Is this the best way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系。对于您的用例来说,它看起来很简单。
如果您遇到多个视图在每个页面中至少有三个实例变量分配和多个部分的情况,那么它就不再简单,您可能需要寻找替代方案。无论如何,您可能想寻找替代方案,只是为了好玩。
That's fine. It looks simple enough for your use case.
If you get to the point where multiple views have at least three instance variable assignments and multiple partials in each page, it stops being simple and you might like to look for an alternative. You might like to look for an alternative anyway, just for fun.