Rails模型,视图,控制器和助手:什么去了哪里? [英] Rails Model, View, Controller, and Helper: what goes where?

查看:75
本文介绍了Rails模型,视图,控制器和助手:什么去了哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby on Rails开发(或通常的MVC)中,关于放置逻辑的位置,我应该遵循什么快速规则.

In Ruby on Rails Development (or MVC in general), what quick rule should I follow as to where to put logic.

请回答肯定-用不要放在这里.

推荐答案

MVC

控制器:在此处放置与确定用户想要的内容,确定给他们的内容,确定他们是否已登录,是否应该查看某些数据等有关的代码.最后,控制器查看请求并确定要显示的数据(模型)和要呈现的视图.如果您不确定代码是否应该放入控制器中,则可能不应该这样做.保持您的控制器皮包.

Controller: Put code here that has to do with working out what a user wants, and deciding what to give them, working out whether they are logged in, whether they should see certain data, etc. In the end, the controller looks at requests and works out what data (Models) to show and what Views to render. If you are in doubt about whether code should go in the controller, then it probably shouldn't. Keep your controllers skinny.

视图:视图仅应包含用于显示数据(模型)的最少代码,不应进行大量处理或计算,而应显示通过以下方式计算(或汇总)的数据模型,或由控制器生成.如果您的视图确实需要执行模型或控制器无法完成的处理,则将代码放入帮助器中.视图中的许多Ruby代码使页面标记难以阅读.

View: The view should only contain the minimum code to display your data (Model), it shouldn't do lots of processing or calculating, it should be displaying data calculated (or summarized) by the Model, or generated from the Controller. If your View really needs to do processing that can't be done by the Model or Controller, put the code in a Helper. Lots of Ruby code in a View makes the pages markup hard to read.

模型:您的模型应位于与数据相关的代码的所有所有位置(构成您网站的实体,例如用户,帖子,帐户,朋友等). )生活.如果代码需要保存,更新或汇总与您的实体相关的数据,请放在此处.它将在您的视图和控制器中重复使用.

Model: Your model should be where all your code that relates to your data (the entities that make up your site e.g. Users, Post, Accounts, Friends etc.) lives. If code needs to save, update or summarise data related to your entities, put it here. It will be re-usable across your Views and Controllers.

这篇关于Rails模型,视图,控制器和助手:什么去了哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆