Ruby on Rails - 加载侧栏内容/块的最佳实践/方法

发布于 2024-10-28 04:39:11 字数 967 浏览 5 评论 0原文

我正在开发一个社区/新闻文章网站,其中几乎所有页面上都有一个带有不同“块”的侧栏。在这些块中是“最近的文章(显示最近的五篇文章)”、“最近的博客”、“最近的评论”,您明白了。

当我开始构建应用程序时,我不确定将控制器代码放在哪里(例如,调用 @recent_articles = Article.where...等)。我不认为它可以进入文章控制器,因为它并不总是被调用的文章控制器。所以我认为它在应用程序控制器中效果最好,因为网站上的大多数内容都会调用它。我将“@recent_content”放入应用程序控制器中,执行 :before_filter 来加载它。

您可能会发现其中的缺陷。随着我对 Rails 的使用越来越好,我又重新进行了重构,因为网站加载速度非常糟糕,果然,由 before_filter 定义的应用程序控制器中的所有逻辑都在每个操作上加载,无论如何是否需要。 (当我清理应用程序控制器上的空间时,该站点的速度显着加快)。

我的错误已被认识到,但我仍然需要在某个地方为 @recent_articles、@recent_blogs 等定义实例变量,以便它们仅在需要时才加载。当然,我最终会在网站内容投入生产时对其进行缓存,但我想成为一名优秀的 Rails 程序员。

所以这是我的问题......您将如何处理这种情况以及您将把逻辑放在哪里?我可以想到两种方法,不确定哪种方法更好...

第一种方法...我查看了另一个 Rails 开发人员的项目,我注意到他通过在 /lib 中创建文件来做类似这样的奇怪事情文件夹。例如,定义页面元标记或活动菜单状态的方法。老实说,我以前没有弄乱 /lib 文件夹,认为我的大部分内容应该保留在 /app 文件夹中。

第二种方式......在我看来,助手似乎是可行的方法。也许我可以定义一个“recent_articles”助手,在其中调用我的 @recent_articles 实例变量,然后渲染并将结果传递到我的共享文件夹中的视图文件。

总的来说,从性能或最佳实践的角度来看,这些方法中哪一种是更好的方法?或者有我不知道的更好的方法吗?

I am developing a community / news article website where there is a side column with different "blocks" on nearly all pages. In these blocks is "Recent Articles (showing five most recent articles)," "Recent Blogs," "Recent Comments," you get the drift.

When I started out building the application, I wasn't real sure where to put the controller code (say, to call @recent_articles = Article.where...etc). I didn't think it could go into the Articles controller, because it's not always the Articles controller being called. So I thought it would work best in the application controller, as most content on the site would be calling this. I put "@recent_content" into the application controller, did a :before_filter to load it.

You might see the flaw in this. As I'm getting better with Rails, I went back to refactor as the site was loading horribly and sure enough, all my logic in the application controller defined by before_filter was being loaded on every action, no matter if it was needed or not. (The site sped up dramatically when I cleaned house on the application controller).

My mistake is realized, but I still need to define the instance variables for @recent_articles, @recent_blogs, etc somewhere, so they load up only when needed. Granted I'll be eventually caching the site content when it goes into production, but I want to be a good Rails programmer here.

So here is my question...exactly how would you handle this situation and where would you put the logic? I can think of two ways, not sure which one is better...

The first way...I took a look at a project from another Rails developer and I noticed he was doing odd things like this by creating files in the /lib folder. For example, defining a method for page meta tags or active menu states. I honestly haven't messed with the /lib folder before, figuring most of my stuff should stay in the /app folder.

The second way...seems to me like helpers might seem the way to go. Maybe I could define a "recent_articles" helper, call my @recent_articles instance variable in there, then render and pass the results to a view file in my shared folder.

Overall, which one of these ways is the better way to go, either from a performance or best-practices viewpoint? Or is there a better way of doing this that I'm unaware of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-11-04 04:39:11

每当有许多模型可以调用特定方法时,我可能会使用一个模块。我认为这就是您在第一个想法中所讨论的内容,因为 /lib 是放置模块的地方。

您也可以使用帮助程序,但最好将逻辑排除在帮助程序之外,仅在模型中(如果可能的话)。助手应该仅用作呈现数据的方式,它们是视图的一部分。如果添加了逻辑,那么就会出现问题:)

确保控制器中也没有逻辑。我一开始也会做同样的事情,但这确实是一个坏主意。相反,将所有内容放入模型中,或者如果它们似乎被许多其他模型使用,则可以将其放入模块中。

希望对你有一点帮助:)

Whenever there are many models that can call a particular method, i would probably use a module. I think that is what you are talking about in your first idea, since /lib is where modules are placed.

You can use helpers as well, but it's a good idea to keep logic out of helpers, only in models if possible. Helpers should be just used as a way to present data, they are part of views. If logic is added, then something is wrong :)

Make sure that you do not have logic in your controllers as well. I would be doing the same things in the beginning, but it's really a bad idea. Instead, put everything in your models, or maybe a module if they seem to be used by many other models.

Hope that helps you a bit :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文