cheatsheets/_output/rails-routes.html

199 lines
4.8 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<link href="style.css" rel="stylesheet" />
</head>
<body>
<h1>Rails-routes</h1>
<h2>mapping</h2>
<p><a href="http://guides.rubyonrails.org/routing.html">Guides/Routing</a></p>
<p><a href="Rhttp://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html">ActionDispatch::Routing::Mapper</a>
(See included modules)</p>
<h3>Multiple resources</h3>
<pre><code>resources :books
# PhotosController:
# index =&gt; GET /photos
# new =&gt; GET /photos/new
# create =&gt; POST /photos/new
# show =&gt; GET /photos/:id
# edit =&gt; GET /photos/:id/edit
# update =&gt; PUT /photos/:id
# delete =&gt; DELETE /photos/:id
#
# Helpers:
# new_book_path
# book_path(id)
# edit_book_path(id)
#
resources :photos do
member { get 'preview' } # /photo/1/preview
get 'preview', on: :member # (..same as the first)
collection { get 'search' } # /photos/search
end
</code></pre>
<h3>Single resource</h3>
<pre><code>resource :coder
# CodersController:
# new =&gt; GET /coder/new
# create =&gt; POST /coder/new
# show =&gt; GET /coder
# edit =&gt; GET /coder/edit
# update =&gt; PUT /coder
# delete =&gt; DELETE /coder
</code></pre>
<h3>Matching</h3>
<pre><code>match 'photo/:id' =&gt; 'photos#show' # /photo/what-is-it
match 'photo/:id', id: /[0-9]+/ # /photo/0192
match 'photo/:id' =&gt; 'photos#show', constraints: { id: /[0-9]+/ }
match 'photo/:id', via: :get
match 'photo/:id', via: [:get, :post]
match 'photo/*path' =&gt; 'photos#unknown' # /photo/what/ever
# params[:format] == 'jpg'
match 'photos/:id' =&gt; 'photos#show', :defaults =&gt; { :format =&gt; 'jpg' }
</code></pre>
<h3>Redirection</h3>
<pre><code>match '/stories' =&gt; redirect('/posts')
match '/stories/:name' =&gt; redirect('/posts/%{name}')
</code></pre>
<h3>Named</h3>
<pre><code># logout_path
match 'exit' =&gt; 'sessions#destroy', as: :logout
</code></pre>
<h3>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>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 &quot;*path&quot; =&gt; &quot;blacklist#index&quot;,
:constraints =&gt; BlacklistConstraint.new
end
</code></pre>
<h3>Scopes</h3>
<pre><code>scope 'admin', constraints: { subdomain: 'admin' } do
resources ...
end
</code></pre>
<h3>Rack middleware</h3>
<pre><code># Yes, Sprockets is middleware
match '/application.js' =&gt; Sprockets
</code></pre>
<h3>Route helpers</h3>
<pre><code>projects_path # /projects
projects_url # http://site.com/projects
</code></pre>
<h3>Default help text</h3>
<pre><code># The priority is based upon order of creation:
# first created -&gt; highest priority.
# Sample of regular route:
match 'products/:id' =&gt; 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
match 'products/:id/purchase' =&gt; 'catalog#purchase', :as =&gt; :purchase
# This route can be invoked with purchase_url(:id =&gt; 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 =&gt; :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 &quot;root&quot;
# just remember to delete public/index.html.
root :to =&gt; 'welcome#index'
# See how all your routes lay out with &quot;rake routes&quot;
# 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>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script src="http://cachedcommons.org/cache/prettify/1.0.0/javascripts/prettify-min.js"></script>
<script>$("pre").addClass("prettyprint");</script>
<script>prettyPrint();</script>
</body>
</html>