Rails 3 应用程序中站点范围的页脚逻辑属于哪里?
我有一个站点范围的页脚,应该显示最近的用户和帖子的列表。我想知道逻辑应该在哪里获取这些数据。我应该在 UsersController 中有一个“recent_users”方法,在 PostsController 中有一个“recent_posts”方法,还是应该有一个单独的 FooterController?
视图/帖子中的 _recent_users 部分视图/用户和 _recent_posts 部分并让页脚部分呈现它们两者怎么样?
I have a site-wide footer that should display a list of recent Users and Posts. I'm wondering where the logic should to gets this data. Should I have a "recent_users" method in the UsersController and a "recent_posts" method in the PostsController, or should I have a separate FooterController?
How about a _recent_users partial views/users and a _recent_posts partial in views/posts and have the footer partial render both of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有“业务逻辑”都应该放在
模型
中,而不是控制器中。最近用户和帖子的查询应该在User
和Post
模型中。然后,如果您有站点范围的视图元素,请将其移至部分视图元素并将该部分视图添加到application.html.erb
中。需要注意的一些重要事项:
不要访问部分中的实例变量(@foo)...将其传递到本地哈希中并将其作为变量访问。这通常是不好的做法
你也可以使用模块
查看缓存,因为你可能不想在每次页面加载时两次访问数据库。您可以在页脚上使用片段缓存,并每 15 分钟过期一次(可能是最好的选择)。
All "business logic" should be put in the
Model
, not the controller. The query for recent Users and Posts should be in theUser
andPost
model. Then, if you have a site-wide view element, move it into a partial and add that partial into theapplication.html.erb
.A few important things to note:
don't access the instance variables (the @foo) in the partial... pass it into the locals hash and access it as a variable instead. It's just generally bad practice
you could also use a module
look into caching because you probably don't want to hit your database TWICE on every page load. You could use fragment caching on the footer and expire it every 15 minutes (probably the best option).