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.