Resourceful URL Helpers in Rails
Published on 31 Mar 2020
Verbs, URLs, Actions, and URL Helpers
Verb | URL | Action | URL Helper |
---|---|---|---|
GET | /subjects | index | subjects_path |
POST | /subjects | create | subjects_path |
GET | /subjects/:id | show | subject_path(:id) |
PATCH | /subjects/:id | update | subject_path(:id) |
DELETE | /subjects/:id | destroy | subject_path(:id) |
GET | /subjects/new | new | new_subject_path |
GET | /subjects/:id/edit/ | edit | edit_subject_path(:id) |
GET | /subjects/:id/delete | delete | delete_subject_path(:id) |
Full path and URL helper
{controller: 'subjects', action: 'show', id: 5}
subject_path(5)
Examples
# /subjects/index
<%= link_to('All Subjects', subjects_path) %>
# /subjects?page=3
<%= link_to('All Subjects', subjects_path(page: 3)) %>
# /subjects/10
## @subject.id = 10
<%= link_to('Show Subject', subject_path(@subject.id)) %>
## .id is optional
<%= link_to('Show Subject', subject_path(@subject)) %>
# /subjects/10?format=verbose
<%= link_to('Show Verbose Subject',
subject_path(@subject.id, format: 'verbose')) %>
# /subjects/10/edit/
<%= link_to('Edit Subject', edit_subject_path(@subject.id)) %>
all tags
activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career cli cloud code coding config configuration cp crud css database db design devops django email erp filter fugitive gif gist git gnome grep hebrew http ide isbn-fetcher iso javascript job search js kanban kanban\ kindle koans linux logger manjaro map markdown microservices mobi mtp neovim nodejs packages pastbin patch post python rails reduce refactoring rest routes rspec ruby scripting security sed shell sql string_replacement study tdd terminal testing version_control vim walkthrough workflow