Router
Rails router is just a piece of code that handles HTTP verb and URL combos and dispatch them to a controller's action.
Route
Route is just the mapping from HTTP verb and URL combo to controller's action.
For example
get '/patients/:id', to: 'patients#show'
Path vs URL
Paths and URLs are easy to be confused.
Say we have a users controller, we can have a path and a URL for it, shown below.
Path => /users
URL => http://www.example.com/users
As we can see here, URLs are just paths prefixed with the current host, port and path prefix.
Resource Routing
In Rails, resource routing allows you to quickly declare all of the common routes for a given resourceful controller by using a single line. For example, you have a controller called photos.
By adding this line into our routes.rb file, we creates the following seven routes.
For example, if Rails application receives a incoming request for
Resource Routing
In Rails, resource routing allows you to quickly declare all of the common routes for a given resourceful controller by using a single line. For example, you have a controller called photos.
resource :photos
By adding this line into our routes.rb file, we creates the following seven routes.
HTTP Verb | Path | Action | Used for |
---|---|---|---|
GET | /photos | index | display a list of all photos |
GET | /photos/new | new | return an HTML form for creating a new photo |
POST | /photos | create | create a new photo |
GET | /photos/:id | show | display a specific photo |
GET | /photos/:id/edit | edit | return an HTML form for editing a photo |
PATCH/PUT | /photos/:id | update | update a specific photo |
DELETE | /photos/:id | destroy | delete a specific photo |
GET /photos/1
The router would dispatch that request to the show action in the photos controller with {id: '1'} in params.
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.Ruby on Rails Online Course
ReplyDelete