{"id":3906,"date":"2015-05-01T20:02:23","date_gmt":"2015-05-02T00:02:23","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3906"},"modified":"2015-05-04T19:45:13","modified_gmt":"2015-05-04T23:45:13","slug":"ruby-on-rails-routing","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-routing\/","title":{"rendered":"Ruby on Rails: Routing"},"content":{"rendered":"<ul>\n<li>Routing connects URLs to code and generates paths from code\n<pre>\/\/ routes.rb ... e.g. directs a call of \/patients\/17 to the show action (method) in the patients controller with a parameter of {id: '17'} ... the 'as' option names the route, creating the patient_path and patient_url helper methods\r\nget '\/patients\/:id', to: 'patients#show', as: 'patient'\r\n\r\n\/\/ controller\r\n@patient = Patient.find(17)\r\n\r\n\/\/ view\r\n&lt;%= link_to 'Patient Record', patient_path(@patient) %&gt;<\/pre>\n<\/li>\n<li>Resource routing allows you to quickly declare all of the <strong>common routes<\/strong> for a given resourceful controller (index, show, new, edit, create, update and destroy actions)\n<pre>resources :controller<\/pre>\n<table class=\"responsive\">\n<thead>\n<tr>\n<th>HTTP Verb<\/th>\n<th>Path<\/th>\n<th>Controller#Action<\/th>\n<th>Used for<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>GET<\/td>\n<td>\/photos<\/td>\n<td>photos#index<\/td>\n<td>display a list of all photos<\/td>\n<\/tr>\n<tr>\n<td>GET<\/td>\n<td>\/photos\/new<\/td>\n<td>photos#new<\/td>\n<td>return an HTML form for creating a new photo<\/td>\n<\/tr>\n<tr>\n<td>POST<\/td>\n<td>\/photos<\/td>\n<td>photos#create<\/td>\n<td>create a new photo<\/td>\n<\/tr>\n<tr>\n<td>GET<\/td>\n<td>\/photos\/:id<\/td>\n<td>photos#show<\/td>\n<td>display a specific photo<\/td>\n<\/tr>\n<tr>\n<td>GET<\/td>\n<td>\/photos\/:id\/edit<\/td>\n<td>photos#edit<\/td>\n<td>return an HTML form for editing a photo<\/td>\n<\/tr>\n<tr>\n<td>PATCH\/PUT<\/td>\n<td>\/photos\/:id<\/td>\n<td>photos#update<\/td>\n<td>update a specific photo<\/td>\n<\/tr>\n<tr>\n<td>DELETE<\/td>\n<td>\/photos\/:id<\/td>\n<td>photos#destroy<\/td>\n<td>delete a specific photo<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>Creating\u00a0a resourceful route will also expose a number of helpers to the controllers in your application&#8230;\n<ul>\n<li>photos_path returns \/photos<\/li>\n<li>new_photo_path returns \/photos\/new<\/li>\n<li>edit_photo_path(:id) returns \/photos\/:id\/edit (for instance, edit_photo_path(10) returns \/photos\/10\/edit)<\/li>\n<li>photo_path(:id) returns \/photos\/:id (for instance, photo_path(10) returns \/photos\/10)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Rail routes are matched in the order you specify<\/li>\n<li>Singular resources map to plural controllers<\/li>\n<li>You can group routes with namespacing\n<pre>namespace :admin do\r\n resources :articles, :comments\r\nend<\/pre>\n<\/li>\n<li>You can have nested routes, but you shouldn&#8217;t go more than one level deep\n<ul>\n<li><span class=\"code\">shallow: true<\/span> will help with clarity when nesting, only building routes with the minimal amount of information to uniquely identify the resource<\/li>\n<\/ul>\n<\/li>\n<li>You can match the verbs get, put, post, patch, and delete to a route with the <span class=\"code\">via:<\/span> option<\/li>\n<li>Use <span class=\"code\">*<\/span> for wild cards in routes<\/li>\n<li>You can redirect routes with the <span class=\"code\">redirect<\/span> helper<\/li>\n<li>Specify the root of your app&#8230;\n<pre>root to: 'pages#main'\r\nroot 'pages#main' # shortcut for the above<\/pre>\n<\/li>\n<li>You can specify the controller for a path with the <span class=\"code\">controller:<\/span> option\n<pre>resources :photos, controller: 'images'<\/pre>\n<\/li>\n<li>Get a list of your routes at\u00a0http:\/\/localhost:3000\/rails\/info\/routes or with\u00a0<span class=\"code\">rake routes<\/span> in your terminal<\/li>\n<li>Three built-in assertions for testing routes&#8230;\n<ul>\n<li>assert_generates &#8230;\u00a0asserts that a particular set of options generate a particular path and can be used with default routes or custom routes\n<pre>assert_generates '\/photos\/1', { controller: 'photos', action: 'show', id: '1' }\r\nassert_generates '\/about', controller: 'pages', action: 'about'<\/pre>\n<\/li>\n<li>assert_recognizes &#8230;\u00a0is the inverse of assert_generates, asserting that a given path is recognized and routes it to a particular spot in your application\n<pre>assert_recognizes({ controller: 'photos', action: 'show', id: '1' }, '\/photos\/1')\r\nassert_recognizes({ controller: 'photos', action: 'create' }, { path: 'photos', method: :post })<\/pre>\n<\/li>\n<li>assert_routing &#8230;\u00a0checks the route both ways\n<pre>assert_routing({ path: 'photos', method: :post }, { controller: 'photos', action: 'create' })<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/routing.html\" target=\"_blank\">Rails Routing from the Outside in<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Routing connects URLs to code and generates paths from code \/\/ routes.rb &#8230; e.g. directs a call of \/patients\/17 to the show action (method) in the patients controller with a parameter of {id: &#8217;17&#8217;} &#8230; the &#8216;as&#8217; option names the route, creating the patient_path and patient_url helper methods get &#8216;\/patients\/:id&#8217;, to: &#8216;patients#show&#8217;, as: &#8216;patient&#8217; \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3906","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3906"}],"version-history":[{"count":9,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3906\/revisions"}],"predecessor-version":[{"id":3916,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3906\/revisions\/3916"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}