About resources that generate RESTful routing for Rails

About RESTful

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.

Routing generated by RESOURCES

Use resources to configure RESTful routing.

routes.rb


resources :xxxs

Routes generated by resources

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

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.

HTTP method

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

About resources that generate RESTful routing for Rails
About Rails routing
About Rails 6
[Ruby on Rails] About bundler (for beginners)
[rails] How to configure routing in resources
Rails Routing Basics
Rails 6.0 Routing Summary
[Rails] About ActiveJob ,!
About Rails controller
About RSpec (Rails)
[Rails] I learned about the difference between resources and resources
About a double loop that puts a For statement inside a For statement