resources :books# BooksController:# index => GET /books# new => GET /books/new# create => POST /books/new# show => GET /books/:id# edit => GET /books/:id/edit# update => PUT /books/:id# delete => DELETE /books/:id## Helpers:# new_book_path# book_path(id)# edit_book_path(id)
Member and Collection
collection is for routes on the collection.
member is for routes on a specific member.
Rails.applications.routes.drawdo resources :eventsdo collection do post :validate# localhost:3000/events/validateend member do post :publish# localhost:3000/events/1/publishendend
resource :coder# CodersController:# new => GET /coder/new# create => POST /coder/new# show => GET /coder# edit => GET /coder/edit# update => PUT /coder# delete => DELETE /coder
scope 'admin',constraints: { subdomain:'admin' } do resources ...end
Nested Resources (routes)
Assuming an event has many registrations and we want registration routes to be nested under an event, e.g. localhost:3000/events/1/registrations, we can do:
First you have to make a new draw method into Rails's routing mapper via an initializer
# config/initializers/routing_draw.rb# Adds draw method into Rails routing# It allows us to keep routing splitted into filesclassActionDispatch::Routing::Mapperdefdraw(routes_name)instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))endend
Update your config/routes.rb with the names of files in config/routes/*.rb