保持控制器清洁

发布于 2024-10-17 21:23:24 字数 421 浏览 2 评论 0原文

假设我有一些逻辑正在对传递到控制器操作的参数哈希执行。我想将逻辑封装在一些方法中,以保持代码易于理解并保持控制器干净。我可以将 Order 模型中的方法作为类级方法。然而,这些方法并不是 Order 模型域职责的真正核心。我的问题是 Rails 中存放这些类型方法的最佳位置在哪里?

OrdersController < ApplicationController
   def update
       # check some conditions here on the params hash...
       # need some methods to do it...
       # where's a good place for these methods other than model or controller?
   end 
end

Let's say I have some logic that's being performed on the params hash passed into a controller action. I'd like to encapsulate the logic in some methods to keep the code understandable and to keep the controller clean. I could put the methods in the Order model as class-level methods. However, these methods are not really core to the domain responsibility of the Order model. My question is where's the best place to house these type of methods in Rails?

OrdersController < ApplicationController
   def update
       # check some conditions here on the params hash...
       # need some methods to do it...
       # where's a good place for these methods other than model or controller?
   end 
end

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

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

发布评论

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

评论(2

檐上三寸雪 2024-10-24 21:23:24

使它们成为控制器的私有实例方法。

Make them private instance methods of the controller.

哽咽笑 2024-10-24 21:23:24

将这些放入 ApplicationHelper 中怎么样?

How about putting these in ApplicationHelper?

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