199 lines
4.8 KiB
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 => 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_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 => GET /coder/new
|
|
# create => POST /coder/new
|
|
# show => GET /coder
|
|
# edit => GET /coder/edit
|
|
# update => PUT /coder
|
|
# delete => DELETE /coder
|
|
</code></pre>
|
|
|
|
<h3>Matching</h3>
|
|
|
|
<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>Redirection</h3>
|
|
|
|
<pre><code>match '/stories' => redirect('/posts')
|
|
match '/stories/:name' => redirect('/posts/%{name}')
|
|
</code></pre>
|
|
|
|
<h3>Named</h3>
|
|
|
|
<pre><code># logout_path
|
|
match 'exit' => '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 "*path" => "blacklist#index",
|
|
:constraints => 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' => 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 -> 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>
|
|
|
|
|
|
<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>
|