Aug 02, 2022 | Ruby
Ruby on Rails (RoR) is a powerful framework for developing any type and complexity level of web applications. One thing that is super easy to do in RoR is routing.
Routes are a way to redirect incoming requests to controllers and actions. It's that simple.
If you have a pages controller with index action, you can specify that in your routes like this:
Rails.application.routes.draw do
get 'pages/index'
end
A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects.
Here are some examples.
Defining a member block in your routes:
Rails.application.routes.draw do
resources :articles do
member do
get 'draft'
end
end
end
This block will generate a route looking like this:
get '/articles/:id/draft', to: 'articles#draft'
# draft_article_path
Defining a collection block in your routes:
Rails.application.routes.draw do
resources :articles do
collection do
get 'search'
end
end
end
This will generate a route looking like this:
get '/articles/search', to: 'articles#search'
# search_articles_path
You don't have to define member and collection routes as blocks.
Here is another way of doing this:
resources :articles do
get 'draft', on: :member
get 'search', on: :collection
end