Rails Model, View, Controller, and Helper: what go

2019-01-08 03:05发布

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

Please answer in the affirmative - With Do put this here, rather than Don't put that there.

10条回答
ゆ 、 Hurt°
2楼-- · 2019-01-08 03:25

Well, it sort of depends upon what the logic has to deal with...

Often, it makes sense to push more things into your models, leaving controllers small. This ensures that this logic can easily be used from anywhere you need to access the data that your model represents. Views should contain almost no logic. So really, in general, you should strive to make it so that you Don't Repeat Yourself.

Also, a quick bit of google reveals a few more concrete examples of what goes where.

Model: validation requirements, data relationships, create methods, update methods, destroy methods, find methods (note that you should have not only the generic versions of these methods, but if there is something you are doing a lot, like finding people with red hair by last name, then you should extract that logic so that all you have to do is call the find_redH_by_name("smith") or something like that)

View: This should be all about formatting of data, not the processing of data.

Controller: This is where data processing goes. From the internet: "The controller’s purpose is to respond to the action requested by the user, take any parameters the user has set, process the data, interact with the model, and then pass the requested data, in final form, off to the view."

Hope that helps.

查看更多
来,给爷笑一个
3楼-- · 2019-01-08 03:27

Perfect explanations here already, one very simple sentence as conclusion and easy to remember:

We need SMART Models, THIN Controllers, and DUMB Views.

http://c2.com/cgi/wiki?ModelViewController

查看更多
在下西门庆
4楼-- · 2019-01-08 03:29

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.

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.

查看更多
5楼-- · 2019-01-08 03:31

In simple terms, generally, Models will have all the codes related to table(s), their simple or complex relationships (think them as sql queries involving multiple tables), manipulation of the data/variables to arrive at a result using the business logic.

Controllers will have code/pointers towards the relevant models for the job requested.

Views will accept the user input/interaction and display the resultant response.

Any major deviation from these will put unwanted strain on that part and the overall application performance may get impacted.

查看更多
登录 后发表回答