Last active
December 18, 2015 01:09
-
-
Save ysadka/5701787 to your computer and use it in GitHub Desktop.
Intro to rails routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails Routes ‘n Shit | |
When your rails app receives an incoming request | |
GET /patients/17 | |
it asks the router to match it to a controller action. | |
match “/patients/:id” => “patients#show” | |
If the first matching route is the request, it is dispatched to the patients controller’s show action with { :id => “17” } as params. | |
RESOURCES creates = | |
GET: index (Display a list of all photos) | |
GET: new (return an HTML form for creating a new photo) | |
POST: create (create a new photo) | |
GET: show (display a specific photo) | |
GET: edit (return an HTML form for editing a photo) | |
PUT: update (update a specific photo) | |
DELETE: destroy (delete a specific photo) | |
Never Cross Some Egret Unless Dead. | |
Never Cross Shad E Unless Desparate. | |
No Church Says Evil Unites Dead. | |
Narrowly Can Some Escape UnDead. | |
ROUTES ARE MATCHED IN THE ORDER THEY ARE SPECIFIED. | |
In the case of resources :photos | |
photo_path returns /photos | |
new_photo_path returns /photos/new | |
edit_photo_path(:id) returns /photo/:id/edit (for instance, edit_photo_path(10) returns /photos/10/edit) | |
photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10) | |
Each of these helpers has a corresponding _url helper (such as photo_url) which returns the same path prefixed with the current host, port, and path prefix. | |
Because the router uses the HTTP verb and IRL to match inbound requests, 4 URLs map to 7 different actions. | |
Singular resources map to plural controllers: /account vs. /accounts/45 | |
A singular resourceful route generates these helpers: | |
new_geocoder_path returns /geocoder/new | |
edit_geocoder_path returns /geocoder/edit | |
geocoder_path returns /geocoder | |
As with plural resources, the same helpers enging in _url will also include the host, port, and path prefix. | |
ROUTE PATHS AND HOW THEY INTERACT | |
WHEN YOU GENERATE, IT ADDS ROUTES TO YOUR CONFIG FILE. | |
get “demo/index” === | |
match “demo/index, :to => “demo#index” | |
DEFAULT ROUTE STRUCTURE LOWEST PRIORITY (Put it at the bottom) | |
:controller/:action/:id | |
GET /students/edit/52 | |
StudentsControllers / edit action | |
match ‘:controller(/:action(:id(.:format)))’ | |
ROUTE ROUTE HIGHEST PRIORITY (Put it at the top) | |
root :to => “demo#index” | |
You can specify a name for any route using the :as option. | |
match ‘exit’ => ‘sessions#destroy’, :as => :logout | |
Optional parameters go in ()’s. | |
Constraints | |
:constraints => { :year => /\d{4}/, :month => /\d{2}/ } etc. | |
:user_agent - specify a specific browser | |
:host - | |
Controllers | |
Controllers are plural. | |
For basic CRUD, On controller per model, plural names, capitalized | |
Allows for clear URLs | |
subjects/new, subject/list | |
pages/new, pages/list | |
sections/new, sections/list | |
How do I know what routes are available in my application? | |
They are all found in route.rb | |
Rake routes in console displays all available routes | |
How do I know what URL helpers Rails will generate for my routes? | |
Creates 7 routes unless you say otherwise. | |
What does the resources method do in routes.rb? What routes does it add and why? | |
Creates 4 gets, 2 post and a delete route | |
Never Cross Some Egret Unless Dead. | |
Never Cross Shad E Unless Desparate. | |
No Church Says Evil Unites Dead. | |
Narrowly Can Some Escape UnDead. | |
New, Create, Show, Edit, Update, and Delete | |
How can I configure what routes the resources method generates? | |
Use only and except. Only will create only those routes, except will create all of the routs except those you specify | |
How do I specify my application's root URL? | |
like this: root_to => '/profile' | |
GOES AT THE TOP... TOP IS HIGHEST PRIORITY.... | |
DEFAULT GOES AT THE BOTTOM | |
What common mistakes do people make when setting up routes in Rails? | |
They define all the routes by hand and don’t remove routes they’re not going to use. | |
What are nested resources? Why do I care about them? Are they worth understanding at the outset? | |
resources :magazines do | |
resources :ads | |
end | |
GET /magazines/:magazine_id/ads | |
http://guides.rubyonrails.org/routing.html#paths-and-urls Section 2.7 | |
creating pathways | |
<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %> | |
What are "member" routes? What are "collection" routes? | |
URL Helper Description | |
---------------------------------------------------------------------------------------------------------------------------------- | |
member /photos/1/preview preview_photo_path(photo) Acts on a specific resource so required id (preview specific photo) | |
collection /photos/search search_photos_url Acts on collection of resources(display all photos) | |
If I memorize only one or two things about routing in Rails, what should that be? | |
Don’t memorize...understand the magic!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment