保持控制器清洁
假设我有一些逻辑正在对传递到控制器操作的参数哈希执行。我想将逻辑封装在一些方法中,以保持代码易于理解并保持控制器干净。我可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使它们成为控制器的私有实例方法。
Make them private instance methods of the controller.
将这些放入 ApplicationHelper 中怎么样?
How about putting these in ApplicationHelper?