As an example, let's consider the case when you want to register as a user and operate a membership-based site.
■ Display the member registration screen (new) → Have them enter and register in the database (create)
■ Display the edit screen (edit) of the registered information → Update the database when there is a change (update)
■ Member list display (index)
■ Display of member details page (show)
■ If a member withdraws, delete it from the database (destroy)
The state where such "a certain resource" can be CRUD operated is called RESTful.
Use resources to configure RESTful routing.
routes.rb
resources :xxxs
URL pattern | action | HTTP method | Explanation | prefix |
---|---|---|---|---|
/XXXs(.:format) | index | GET | Generate list screen | xxxs |
/XXXs(.:format) | create | POST | registration process | |
/XXXs/new(.:format) | new | GET | Generate registration screen | new_xxx |
/XXXs/:id/edit(.:format) | edit | GET | Generate edit screen | edit_xxx |
/XXXs/:id(.:format) | show | GET | Generate details screen | xxx |
/XXXs/:id(.:format) | update | PUT / PATCH | Update process | |
/XXXs/:id(.:format) | destroy | DELETE | Delete process |
Make sure you understand the actions that correspond to HTTP requests and their roles.
HTTP request is a combination of HTTP method and URL. When GET + / xxxs is requested to the Web server, the xxxs controller is instantiated and the index action is executed according to the settings of the router (routes.rb). Since the role of the index action is to render the list screen, this action should be used when displaying the list page.
I'm using 4 in Rails
HTTP method name | meaning |
---|---|
GET | Acquisition of resources (screen display and data request) |
POST | Create new resource |
DELETE | Delete resource |
PUT / PATCH | Resource update |
Recommended Posts