对 Rails 中的 MVC 感到困惑
我对在 Rails 中的什么地方写什么有点困惑。
理想情况下,我将拥有一个视图、一个控制器、一个模型。模型应该具有所有业务逻辑。但在我见过的大多数 Rails 应用程序中,我看到大部分业务逻辑都写在 Controller 文件中。
我们应该称他们为控制器吗?那么视图模型呢?我正在谈论与视图关联的数据模型。当我说这句话时,我的脑海中浮现出 JSF 和 Swing。每个视图都有一个与之关联的数据模型,通常是一个 bean。但我们这里没有这样的东西,还是我无知?那么服务层呢?如何在 Rails 代码中实现它们。
总结我的问题
- 为什么业务逻辑是 大部分都是用控制器写的 轨道代码?这是一个好的做法吗?
- 如何将视图模型合并到 Rails,即视图的数据模型?
- 在 Rails 应用程序中将服务层放在哪里?
谢谢
I am a bit confused on what to write where in Rails.
Ideally, I will be having a view, a controller, a model. Model should be having all the business logic. But in most of the Rails applications I've seen, I've seen most of the business logic written in the Controller files.
Should we call them as controller? And what about View-models. I am talking about the datamodels that are associated for a view. I am having JSF and Swing in my mind when I say this. There every view has a datamodel associated with it, usually a bean. But here, we don't have anything like that or I am ignorant? And what about service layers, How do I implement them in my Rails code.
To summarize my questions
- Why are business logic being
written in Controllers in most of
Rails code? Is this a good practice? - How to incorporate the view-model in
rails, ie, data-models for view? - Where to put service layers in a Rails app?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总结一下我的答案:
Ruby on Rails 的新标准是将业务逻辑放入模型中。这也称为胖模型瘦控制器。
Rails 不会强制执行一种数据模型的这种方法,但您可以选择实现它。基本的脚手架做了类似的事情。
服务层很可能存储在机架中间件中。这允许对请求和响应进行一般过滤。
To summarize my answers:
The new standard for Ruby on Rails is to place business logic in the model. This is also known as fat model skinny controller.
Rails doesn't enforce this approach of one data model but you can implement it if you choose. The basic scaffolding does something similar.
The service layers will most likely be stored within rack middleware. This allows for general filtering of requests and responses.