426 lines
11 KiB
HTML
426 lines
11 KiB
HTML
<!doctype html>
|
|
<html lang='en' class='no-js '>
|
|
<head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta charset='utf-8'>
|
|
<meta content='width=device-width, initial-scale=1.0' name='viewport'>
|
|
<link href='./assets/favicon.png' rel='shortcut icon'>
|
|
<meta content='/rails-routes.html' name='app:pageurl'>
|
|
|
|
|
|
<title>Routes cheatsheet</title>
|
|
<meta content='Routes cheatsheet' property='og:title'>
|
|
<meta content='Routes cheatsheet' property='twitter:title'>
|
|
<meta content='article' property='og:type'>
|
|
|
|
|
|
|
|
<meta content='https://assets.devhints.io/previews/rails-routes.jpg?t=20210218065802' property='og:image'>
|
|
<meta content='https://assets.devhints.io/previews/rails-routes.jpg?t=20210218065802' property='twitter:image'>
|
|
<meta content='900' property='og:image:width'>
|
|
<meta content='471' property='og:image:height'>
|
|
|
|
|
|
|
|
<meta content="The one-page guide to Routes: usage, examples, links, snippets, and more." name="description">
|
|
<meta content="The one-page guide to Routes: usage, examples, links, snippets, and more." property="og:description">
|
|
<meta content="The one-page guide to Routes: usage, examples, links, snippets, and more." property="twitter:description">
|
|
|
|
|
|
<link rel="canonical" href="https://devhints.io/rails-routes">
|
|
<meta name="og:url" content="https://devhints.io/rails-routes">
|
|
|
|
|
|
|
|
|
|
|
|
<meta content='Devhints.io cheatsheets' property='og:site_name'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta content='Rails' property='article:section'>
|
|
|
|
|
|
|
|
|
|
|
|
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-106902774-1'></script>
|
|
<script>
|
|
|
|
window.dataLayer=window.dataLayer||[];
|
|
function gtag(){dataLayer.push(arguments)};
|
|
gtag('js',new Date());
|
|
gtag('config','UA-106902774-1');
|
|
</script>
|
|
|
|
|
|
|
|
<meta property='page:depth' content='1'>
|
|
|
|
|
|
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
|
|
<script>(function(H){H.className=H.className.replace(/\bNoJs\b/,'WithJs')})(document.documentElement)</script>
|
|
|
|
<script>(function(d,s){if(window.Promise&&[].includes&&Object.assign&&window.Map)return;var js,sc=d.getElementsByTagName(s)[0];js=d.createElement(s);js.src='https://cdn.polyfill.io/v2/polyfill.min.js';sc.parentNode.insertBefore(js, sc);}(document,'script'))</script>
|
|
|
|
<!--[if lt IE 9]><script src='https://cdnjs.cloudflare.com/ajax/libs/nwmatcher/1.2.5/nwmatcher.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js'></script><script src='https://cdn.rawgit.com/gisu/selectivizr/1.0.3/selectivizr.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script><![endif]-->
|
|
|
|
<style>html{opacity:0}</style>
|
|
<link rel="stylesheet" href="./assets/2015/style.css?t=20210218065802">
|
|
<link href="./assets/style.css?t=20210218065802" rel="stylesheet" />
|
|
<link href="./assets/print.css?t=20210218065802" rel="stylesheet" media="print" />
|
|
</head>
|
|
<body>
|
|
<div class='all'>
|
|
|
|
<div class='site-header'>
|
|
<div class='container'>
|
|
This is <a href="."><em>Devhints.io cheatsheets</em></a> — a collection of cheatsheets I've written.
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<script type='application/ld+json'>
|
|
{
|
|
"@context": "http://schema.org",
|
|
"@type": "NewsArticle",
|
|
"mainEntityOfPage": {
|
|
"@type": "WebPage",
|
|
"@id": "https://google.com/article"
|
|
},
|
|
"headline": "Routes cheatsheet",
|
|
"image": [ "https://assets.devhints.io/previews/rails-routes.jpg?t=20210218065802" ],
|
|
"description": "The one-page guide to Routes: usage, examples, links, snippets, and more."
|
|
}
|
|
</script>
|
|
<script type='application/ld+json'>
|
|
{
|
|
"@context": "http://schema.org",
|
|
"@type": "BreadcrumbList",
|
|
"itemListElement": [{
|
|
"@type": "ListItem",
|
|
"position": 1,
|
|
"item": {
|
|
"@id": "https://devhints.io/#rails",
|
|
"name": "Rails"
|
|
}
|
|
},{
|
|
"@type": "ListItem",
|
|
"position": 2,
|
|
"item": {
|
|
"@id": "https://devhints.io/rails-routes",
|
|
"name": "Routes"
|
|
}
|
|
}]
|
|
}
|
|
</script>
|
|
|
|
|
|
<div class='post-list -single -cheatsheet'>
|
|
<div class='post-item'>
|
|
<div class='post-headline -cheatsheet'>
|
|
<p class='prelude'><span></span></p>
|
|
<h1><span>Routes</span></h1>
|
|
|
|
<div class='pubbox'>
|
|
<div class='HeadlinePub' role='complementary'>
|
|
<script async src='https://pubsrv.devhints.io/carbon.js?serve=CE7IK5QM&placement=devhintsio&cd=pubsrv.devhints.io/s' id="_carbonads_js"></script>
|
|
<span class='placeholder -one'></span>
|
|
<span class='placeholder -two'></span>
|
|
<span class='placeholder -three'></span>
|
|
<span class='placeholder -four'></span>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class='post-content -cheatsheet'>
|
|
<h2 id="multiple-resources-resources">Multiple resources (<code>resources</code>)</h2>
|
|
|
|
<pre><code>resources :photos
|
|
|
|
# PhotosController:
|
|
# index => GET /photos
|
|
# new => GET /photos/new
|
|
# create => POST /photos/new
|
|
# show => GET /photos/:id
|
|
# edit => GET /photos/:id/edit
|
|
# update => PUT /photos/:id
|
|
# delete => DELETE /photos/:id
|
|
#
|
|
# Helpers:
|
|
# new_photo_path
|
|
# photo_path(id)
|
|
# edit_photo_path(id)
|
|
</code></pre>
|
|
|
|
<h3 id="custom-actions">Custom actions</h3>
|
|
|
|
<pre><code>resources :photos do
|
|
member { get 'preview' } # /photo/1/preview
|
|
collection { get 'search' } # /photos/search
|
|
|
|
get 'preview', on: :member # (..same as the first)
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="options">Options</h3>
|
|
|
|
<pre><code>resources :photos,
|
|
path_names: { new: 'brand_new' } # /photos/1/brand_new
|
|
path: 'postings' # /postings
|
|
only: :index
|
|
only: [:index, :show]
|
|
except: :show
|
|
except: [:index, :show]
|
|
|
|
shallow: true # also generate shallow routes
|
|
shalow_path: 'secret'
|
|
shallow_prefix: 'secret'
|
|
</code></pre>
|
|
|
|
<h2 id="single-resource-resource">Single resource (<code>resource</code>)</h2>
|
|
|
|
<pre><code>resource :coder
|
|
|
|
# CodersController:
|
|
# new => GET /coder/new
|
|
# create => POST /coder/new
|
|
# show => GET /coder
|
|
# edit => GET /coder/edit
|
|
# update => PUT /coder
|
|
# delete => DELETE /coder
|
|
</code></pre>
|
|
|
|
<h2 id="matching-match">Matching (<code>match</code>)</h2>
|
|
|
|
<pre><code>match 'photo/:id' => 'photos#show' # /photo/what-is-it
|
|
match 'photo/:id', id: /[0-9]+/ # /photo/0192
|
|
match 'photo/:id' => 'photos#show', constraints: { id: /[0-9]+/ }
|
|
match 'photo/:id', via: :get
|
|
match 'photo/:id', via: [:get, :post]
|
|
|
|
match 'photo/*path' => 'photos#unknown' # /photo/what/ever
|
|
|
|
# params[:format] == 'jpg'
|
|
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
|
|
</code></pre>
|
|
|
|
<h3 id="getpost">Get/post</h3>
|
|
|
|
<p><code>get</code> is the same as <code>match via: :get</code>.</p>
|
|
|
|
<pre><code>get 'photo/:id' => 'photos#show'
|
|
# same as match 'photo/:id' => 'photos#show', via: :get
|
|
|
|
post 'photo/:id' => 'photos#update'
|
|
# same as match 'photo/:id' => 'photos#show', via: :post
|
|
</code></pre>
|
|
|
|
<h3 id="redirection">Redirection</h3>
|
|
|
|
<pre><code>match '/stories' => redirect('/posts')
|
|
match '/stories/:name' => redirect('/posts/%{name}')
|
|
</code></pre>
|
|
|
|
<h3 id="named">Named</h3>
|
|
|
|
<pre><code># logout_path
|
|
match 'exit' => 'sessions#destroy', as: :logout
|
|
</code></pre>
|
|
|
|
<h3 id="constraints">Constraints</h3>
|
|
|
|
<pre><code>match '/', constraints: { subdomain: 'admin' }
|
|
|
|
# admin.site.com/admin/photos
|
|
namespace 'admin' do
|
|
constraints subdomain: 'admin' do
|
|
resources :photos
|
|
end
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="custom-constraints">Custom constraints</h3>
|
|
|
|
<pre><code>class BlacklistConstraint
|
|
def initialize
|
|
@ips = Blacklist.retrieve_ips
|
|
end
|
|
|
|
def matches?(request)
|
|
@ips.include?(request.remote_ip)
|
|
end
|
|
end
|
|
|
|
TwitterClone::Application.routes.draw do
|
|
match "*path" => "blacklist#index",
|
|
:constraints => BlacklistConstraint.new
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="scopes">Scopes</h3>
|
|
|
|
<pre><code>scope 'admin', constraints: { subdomain: 'admin' } do
|
|
resources ...
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="rack-middleware">Rack middleware</h3>
|
|
|
|
<pre><code># Yes, Sprockets is middleware
|
|
match '/application.js' => Sprockets
|
|
</code></pre>
|
|
|
|
<h3 id="route-helpers">Route helpers</h3>
|
|
|
|
<pre><code>projects_path # /projects
|
|
projects_url # http://site.com/projects
|
|
</code></pre>
|
|
|
|
<h3 id="default-help-text">Default help text</h3>
|
|
|
|
<pre><code># The priority is based upon order of creation:
|
|
# first created -> highest priority.
|
|
|
|
# Sample of regular route:
|
|
match 'products/:id' => 'catalog#view'
|
|
|
|
# Keep in mind you can assign values other than :controller and :action
|
|
|
|
# Sample of named route:
|
|
match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
|
|
|
|
# This route can be invoked with purchase_url(:id => product.id)
|
|
|
|
# Sample resource route (maps HTTP verbs to controller actions automatically):
|
|
resources :products
|
|
|
|
# Sample resource route with options:
|
|
resources :products do
|
|
member do
|
|
get 'short'
|
|
post 'toggle'
|
|
end
|
|
|
|
collection do
|
|
get 'sold'
|
|
end
|
|
end
|
|
|
|
# Sample resource route with sub-resources:
|
|
resources :products do
|
|
resources :comments, :sales
|
|
resource :seller
|
|
end
|
|
|
|
# Sample resource route with more complex sub-resources
|
|
resources :products do
|
|
resources :comments
|
|
resources :sales do
|
|
get 'recent', :on => :collection
|
|
end
|
|
end
|
|
|
|
# Sample resource route within a namespace:
|
|
namespace :admin do
|
|
# Directs /admin/products/* to Admin::ProductsController
|
|
# (app/controllers/admin/products_controller.rb)
|
|
resources :products
|
|
end
|
|
|
|
# You can have the root of your site routed with "root"
|
|
# just remember to delete public/index.html.
|
|
root :to => 'welcome#index'
|
|
|
|
# See how all your routes lay out with "rake routes"
|
|
|
|
# This is a legacy wild controller route that's not recommended for RESTful applications.
|
|
# Note: This route will make all actions in every controller accessible via GET requests.
|
|
match ':controller(/:action(/:id(.:format)))'
|
|
</code></pre>
|
|
|
|
<h3 id="references">References</h3>
|
|
|
|
<ul>
|
|
<li>
|
|
<p><a href="http://guides.rubyonrails.org/routing.html">Guides/Routing</a></p>
|
|
</li>
|
|
<li>
|
|
<p><a href="http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html">ActionDispatch::Routing::Mapper</a>
|
|
(See included modules)</p>
|
|
</li>
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<ul class="social-list ">
|
|
<li class="facebook link hint--bottom" data-hint="Share on Facebook"><a href="https://www.facebook.com/sharer/sharer.php?u=https://devhints.io/rails-routes.html" target="share"><span class="text"></span></a></li>
|
|
<li class="twitter link hint--bottom" data-hint="Share on Twitter"><a href="https://twitter.com/intent/tweet?text=The%20ultimate%20cheatsheet%20for%20Routes.%20https://devhints.io/rails-routes.html" target="share"><span class="text"></span></a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class="about-the-site">
|
|
<div class="container">
|
|
<p class='blurb'>
|
|
<strong><a href=".">Devhints.io cheatsheets</a></strong> is a collection of cheatsheets I've written over the years.
|
|
Suggestions and corrections? <a href='https://github.com/rstacruz/cheatsheets/issues/907'>Send them in</a>.
|
|
<i class='fleuron'></i>
|
|
I'm <a href="http://ricostacruz.com">Rico Sta. Cruz</a>.
|
|
Check out my <a href="http://ricostacruz.com/til">Today I learned blog</a> for more.
|
|
</p>
|
|
|
|
|
|
<p class='back'>
|
|
<a class='big-button -back -slim' href='.#toc'></a>
|
|
</p>
|
|
|
|
|
|
<p>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/highlight.min.js"></script>
|
|
|
|
|
|
|
|
|
|
<script src="https://cdn.rawgit.com/rstacruz/unorphan/v1.0.1/index.js"></script>
|
|
<script>hljs.initHighlightingOnLoad()</script>
|
|
<script>unorphan('h1, h2, h3, p, li, .unorphan')</script>
|
|
|
|
</body>
|
|
</html>
|
|
|