To understand the design structure of the Ruby on Rails Framework _ruby topic in the way of MVC thinking

Source: Internet
Author: User
Tags ruby on rails

In rails, the process of sending a request (/users) is as follows:
1), Browser send request (/users)
2), rails routes will route the request to the Users_controller index method
3), Users_controller callback use user model to get all user
4, user model will be from the database all the user read out,
5, user model to read from the database all user package for the list returned to User_controller
6), User_controlle the user model back to the user list assignment to the instance variable @users, the variable will be passed to the index view
7, the index view receives the @users passed over and renders the view to HTML via embedded Ruby
8), User_controller to return the rendered page to the browser.

The different request processing process is basically the same, in addition to the routing strategy, that is, call the different controller, or controller the various methods, the specific call strategy by the rails router decision.

Rails Router
The resource routing strategy in Rails completely adheres to the rest design style, where the URL is only responsible for locating the resource, and the operation of the resource is determined by the native HTTP method type, which is configured with the resources in the ROUTES.RB only: Users, you can get a range of routing strategies as follows:

HTTP request  URI    Action   purpose 
get     /users   index  page to list all users 
get     / USERS/1   Show    page of user with ID 1 get     /users/new  new page to make    a new user 
PO ST     /users    Create   Create a new user 
get     /users/1/edit edit    page to edit user with ID 1 
put     /users/1   update   update user with ID 1 
Delete   /users/1   destroy  Delete User with ID 1 

Of course, in addition to the rest-style routes, rails also supports a variety of custom routes rules, as follows: Add statements to the ROUTES.RB

 
 


The rule matches the browser browsing/user/create,rails will route the request to the Users_controller new method, and the other processing is the same as/users/new. Note, however, that rails uses the first matching rule if we change the above route statement to

Match ' users/create ' => ' users#new ': via =>: Get 

, it would not work as we had imagined, and it would match up to the rules

Get    /users/1   show    page "user with ID 1"


Gets the user with ID created.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.