绕过 Rails 中的控制器

发布于 2024-11-24 10:11:13 字数 828 浏览 1 评论 0原文

我使用 Rails 2.3.5,我有一个模型,我们称之为 Post。我使用命名范围在 Post 上应用不同类型的排序。例如,在 Post 模型中,我可以按分数对帖子进行排名:

named_scope :order_by_acception_rate_desc,
Proc.new { |limit| { :limit => limit, :order => "acception_rate desc" } }

在 Post 控制器中,我有:

def best
    @best_posts = Post.order_by_acception_rate_desc(10)
end

在视图中,我只是渲染此集合 @best_posts:

<%= render :partial => "post", :collection => @best_posts

目前我的应用程序正在这样工作,但实际上我不需要在控制器中具有“最佳”方法,我可以将其移动到模型帖子中,如下所示:

def self.best
    self.order_by_acception_rate_desc(10)
end

然后在视图中我将呈现集合,如下所示:

<%= render :partial => "post", :collection => Post.best

我不知道哪种方法更好,但使用中的排名方法模型,我可以避免为每个排名创建路线 方法。什么方法更好,还有比这些更好的方法吗?

Im using Rails 2.3.5 and I have a model, let's call it Post. I used named scopes to apply different kinds of sorts on Post. For example, in the Post model I have possibility to rank posts by its scores:

named_scope :order_by_acception_rate_desc,
Proc.new { |limit| { :limit => limit, :order => "acception_rate desc" } }

In the Post Controller I have:

def best
    @best_posts = Post.order_by_acception_rate_desc(10)
end

In the view I just render this collection @best_posts:

<%= render :partial => "post", :collection => @best_posts

Currently my application is working like that, but actually I do not need to have the method "best" in the Controller, and I could move it to the Model Post doing like:

def self.best
    self.order_by_acception_rate_desc(10)
end

and then in the view I would render the collection like:

<%= render :partial => "post", :collection => Post.best

I do not know which approach is better, but using the ranking methods in the Model, I could avoid to create routes for each one of ranking methods. What approach is better, is there any better approach than these?

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

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

发布评论

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

评论(3

稀香 2024-12-01 10:11:13

根据 Rails 约定,逻辑应该分开,

  1. 控制器处理权限、身份验证/授权、分配实例/类变量
  2. 帮助器处理 html 逻辑向用户视图显示/隐藏什么
  3. 不应该提供任何逻辑、权限检查。从设计者的角度考虑
  4. 模型通过 ORM 处理数据收集/操作

我想请你尝试一下:

#helper
def self.best(limit)
  all(:limit => limit, :order => "acception_rate desc")
end

#controller
@best_posts = Post.best

#view
<%= render :partial => "post", :collection => @best_posts %>

with according to Rails conventions the logic should be separated,

  1. controllers handle permissions, auth/authorization, assign instance/class variables
  2. helpers handle html logic what to show/hide to user
  3. views should not provide any logic, permissions check. think about it from designer's point of view
  4. models handle data collection/manipulation over ORM

I'd like to ask you to try:

#helper
def self.best(limit)
  all(:limit => limit, :order => "acception_rate desc")
end

#controller
@best_posts = Post.best

#view
<%= render :partial => "post", :collection => @best_posts %>
爱的十字路口 2024-12-01 10:11:13

您不应该绕过控制器并在视图中包含太多逻辑。

您可以保留单个路由并根据params之一过滤Post模型。

您在这里没有说得足够多,无法更清楚地回答,但您已经了解了大局。

You should not bypass the controller and include much logic in your view.

You could keep a single route and filter the Post model depending on one of the params.

You don't tell enough here to answer more clearly but you have the big picture.

可是我不能没有你 2024-12-01 10:11:13

您可以只保留视图文件,它应该可以工作。

You can leave just the view file and it should work.

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