From: http://hi.baidu.com/waytofall/blog/item/dad928f9d042b65f242df222.html
Introduction to the MVC Architecture in Ruby on Rails
MVC was first proposed by Trygve reenskaug (seemingly a Norwegian, Professor at the University of Oslo, claiming to be a self-taught Professional Software Tool Developer. It was initially applied to the development of user interfaces, but the development that is widely used in Web applications is only a matter of recent years. MVC, as a design pattern, can be used by all frameworks and platforms, is not exclusive to Ruby on Rails. Why is MVC particularly important in Ruby on Rails? The reason is that Ruby on Rails uses a fixed directory structure (of course there may be other mechanisms that I don't know) to force developers to follow the MVC design pattern. Ruby on Rails is a development framework with "Convention over configuration", so you can focus on applications without unnecessary configuration cumbersome.ProgramDevelopment is obviously a big pleasure. Of course, MVC also has its advantages in Web application development, so rubyon rails is currently so popular.
About MVC, this article only talks about how MVC is applied in the Ruby on Rails framework, so it discusses the simplest rails development scenarios. The advantage of doing so is to give beginners who are not familiar with MVC and Ruby on Rails a general recognition of them as soon as possible, this allows you to quickly get involved in Ruby on Rails development.
MVC Introduction
First, we need to repeat what MVC is.
M: Model
V: View
C: Controller
ModelThe layer is closely related to the database and the core of the entire application design. The model can be understood as the objects in all applications and various operations encapsulated in them. The Ruby on Rails framework keeps the idea of object-oriented throughout, so the center around all the operations of the model is of course.
ViewIt is a layer of dealing with users, in fact, some are embedded in rubyCode. You can use view to apply for some operations on web applications and view the operation results.
ControllerA layer may be a layer that is hard to express. It is usually used to connect the model layer and view layer. The controller can be understood as a set of actions that serve the interaction between the view and the model (the view requests data from the model and the model returns data to the view, the controller can also process the data. More specific implementation problems will be discussed later.
Why MVC?
Through the above expressions, readers can have a general understanding of MVC, but they may not fully understand the advantages of MVC. So I will explain the advantages of MVC.
The essence of Web applications is that users perform various operations on website information through webpages, sometimes reading, sometimes writing, and sometimes more complex operations, for example, sort the information by category or perform some processing before returning it to the user. All of this information is stored in the server's database (a very small amount of information is stored locally, such as cookies ). This means that the essence of Web application development is to extract user behavior, read information from the database, and return it to the user. At this level, web application development is a collection of user behavior capturing, database operations, and display information. However, if you put all these operations in a file, it will not only be difficult to organize, but also have poor readability and structure, and it is not conducive to code reuse. Therefore, the MVC Architecture is used to separate these chaotic codes to make the structure clearer, make the code easier to read, and improve its reusability and abstraction.
Model is an abstract class and a data entity. View extracts user behaviors and displays data to users. While the Controller is responsible for connecting them, so that the M-V can efficiently coordinate the work.
How rails join them together
Now, readers have a deep understanding of model and view, but may be confused about controller. The question about how the Controller connects m and V is also vague. This chapter will focus on this issue. First, let's first look at how rails runs a web application.
In the rails project folder, there is an app directory that stores all M-V-C code. Model directory storage model, controller directory storage controller, view directory storage view. There are also many subdirectories in the view directory, which correspond to a controller. Many views are stored in these subdirectories, which correspond to an action in the corresponding controller (the controller is composed of one action ).
When we open a rails page, the mechanism is actually like this: the server calls the action in a controller and opens the view corresponding to this action. The variables in the corresponding action can be used for the view. Where do these variables come from?
Let's talk about the model further. As mentioned before, model is actually some classes and Operations corresponding to these classes. What are the functions of these classes? In fact, most of the time, each class of the model corresponds to a table in the database. If you open the model file, you cannot see the definition of attributes in the class. Where are these attributes? The answer is database. The rails framework automatically obtains the attributes of a model based on the columns in the database. Therefore, the relationship between the model and the database is so close.
After knowing this, answer the previous question: the variable in action comes from model. This is another reason why the controller can connect m and V: the controller can call the model object and its method.
Let's summarize the working mechanism of MVC: the Controller receives requests from the view, accesses the objects in the corresponding model, calls the method in the model to return the data processing to the view, and then displays the data, that's all.
If the above is not simple enough, you can imagine eating in a restaurant: the customer orders food from the waiter, and the waiter asks the cook to cook according to the food the customer ordered, the waiter then delivers the food to the customer's dinner table. The customer is view, the waiter is controller, and the cook is model.