De Rails-router herkent URL's en stuurt deze door naar de actie van een controller, terwijl tegelijkertijd paden en URL's worden gegenereerd via helper-functies.
Resourcegerichte routes (resources)
Rails.application.routes.draw do
root "home#index"
resources :articles do
# Member-routes (werkt op specifiek item /articles/:id/publish)
member do
patch :publish
end
# Collection-routes (werkt op /articles/search)
collection do
get :search
end
# Geneste resources (/articles/:article_id/comments)
resources :comments, only: [:create, :destroy]
end
end
Routepad-helpers (Route Path Helpers)
| Actie | Pad-helper | Gegenereerde URL |
|---|---|---|
index |
articles_path |
/articles |
show |
article_path(@article) |
/articles/:id |
new |
new_article_path |
/articles/new |
edit |
edit_article_path(@article) |
/articles/:id/edit |
Namespaces & Scopes
# Namespace: URL-voorvoegsel /admin en controller-voorvoegsel Admin::
namespace :admin do
resources :users
end
# Verwijst naar Admin::UsersController op /admin/users
# Scope: URL-voorvoegsel zonder wijziging van controller-namespace
scope "/v1" do
resources :posts
end
Aangepaste enkelvoudige routes & omleidingen
# Aangepaste HTTP-werkwoordkoppeling
get '/about', to: 'pages#about'
post '/webhook', to: 'webhooks#receive'
# 301 Permanente omleiding
get '/stories', to: redirect('/articles')