'welcome#show' # Regular routes match 'products/:id', :to => 'catalog#view' match 'products/:id' => 'catalog#view' # Named routes match 'logout', :to => 'sessions#destroy', :as => 'logout' match 'logout' => 'sessions#destroy', :as => 'logout' # Verb routes match 'account/overview', :to => 'account#overview', :via => 'get' get 'account/overview' part I mercoledì 23 maggio 12
collection do get :sold post :export end member do post :publish end put :toggle, :on => :member end # Nested resourceful routes resources :projects do resources :tasks, :people end part II mercoledì 23 maggio 12
/photos/long/path/to/12, # setting params[:other] to "12" or "long/path/to/12" match 'photos/*other' => 'photos#unknown' # This would match books/some/section/last-words-a-memoir with # params[:section] equals to "some/section", # and params[:title] equals to"last-words-a-memoir" match 'books/*section/:title' => 'books#show' mercoledì 23 maggio 12
redirected to the create action in posts controller, # but /posts/edit will not work: match 'posts(/new)', :to => 'posts#create' # This is an optional path scope that allows to have a prepended path before a resource: scope '(:locale)', :locale => /en|it/ do resources :descriptions end # Default generic Rails 3 route match '/:controller(/:action(/:id))' mercoledì 23 maggio 12
to /bar/2s: match '/foo/:id', :to => redirect("/bar/%{id}s") # This code will redirect /account/proc/john to /johns. match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" } # Constraint on numeric id match '/posts/show/:id', :to => 'posts#index', :constraints => {:id => /\d/} # Constraint on subdomain match '/foo/bar', :to => 'foo#bar', :constraints => {:subdomain => 'support'} # Constraint on ip address match '/foo/bar', :to => 'foo#bar', :constraints => {:ip => /127.0.0.1/} mercoledì 23 maggio 12
do match '/questions', :to => redirect('http://www.stackoverflow.com/') end # Object-based constraint class IpRestrictor def self.matches?(request) request.ip =~ /127.0.0.1/ end end constraints IpRestrictor do match '/questions', :to => redirect('http://www.stackoverflow.com/') end mercoledì 23 maggio 12
supporting Ruby and Ruby frameworks. class MySinatraBlogApp < Sinatra::Base get "/blog/archives" do "my old posts" end end match "/blog" => MySinatraBlogApp, :anchor => false mount MySinatraBlogApp, :at => "/blog" # or simply mount MySinatraBlogApp => "/blog" part I mercoledì 23 maggio 12
root :to => 'sessions#new', :constraints => LoggedInConstraint.new(false) root :to => 'users#show', :constraints => LoggedInConstraint.new(true) # In config/initializers/logged_in_constraint.rb class LoggedInConstraint < Struct.new(:value) def matches?(request) request.cookies.key?("auth_token") == value end end part II mercoledì 23 maggio 12
Rails.root.join('config/routes/api_routes.rb') # In config/routes/api_routes.rb Sandbox::Application.routes.draw do constraints(:subdomain => 'api') do resources :recipes end end ### Coming in Rails 4 ### # config/routes.rb draw :admin # config/routes/admin.rb namespace :admin do resources :posts end mercoledì 23 maggio 12
:content, :name, :title def put_routes posts_path end end 1.9.3p194 :001 > Post.first.put_routes Post Load (0.3ms) SELECT `posts`.* FROM `posts` LIMIT 1 NameError: undefined local variable or method `posts_path' for #<Post:0x007ff1a17f4e60> part I mercoledì 23 maggio 12
:content, :name, :title include Rails.application.routes.url_helpers default_url_options[:host] = 'example.com' def put_routes posts_path end end 1.9.3p194 :001 > Post.first.put_routes Post Load (0.3ms) SELECT `posts`.* FROM `posts` LIMIT 1 => "/posts" 1.9.3p194 :002 > Post.first.put_instance_route Post Load (0.6ms) SELECT `posts`.* FROM `posts` LIMIT 1 => "http://example.com/posts/1" def put_instance_route url_for(self) end include ActionDispatch::Routing::UrlFor part II mercoledì 23 maggio 12
:content, :name, :title include ActionDispatch::Routing::UrlFor include Rails.application.routes.url_helpers default_url_options[:host] = 'example.com' def self.put_routes Rails.application.routes.url_helpers.posts_path end end 1.9.3p194 :001 > Post.put_routes NameError: undefined local variable or method `posts_path' for #<Class:0x007fcb9b1260d8> class Post < ActiveRecord::Base attr_accessible :content, :name, :title include ActionDispatch::Routing::UrlFor include Rails.application.routes.url_helpers default_url_options[:host] = 'example.com' def self.put_routes posts_path end end 1.9.3p194 :001 > Post.put_routes => "/posts" part III mercoledì 23 maggio 12
undefined local variable or method `posts_path' for main:Object 1.9.3p194 :002 > include Rails.application.routes.url_helpers => Object 1.9.3p194 :003 > posts_path => "/posts" mercoledì 23 maggio 12
"Example of routes helper in rake task" task :posts => :environment do puts "items_path = #{posts_path}" end end @Pade ➜ rake example:posts rake aborted! undefined local variable or method `posts_path' for main:Object namespace :example do desc "Example of routes helper in rake task" task :posts => :environment do include Rails.application.routes.url_helpers puts "items_path = #{posts_path}" end end @Pade ➜ rake example:posts items_path = /posts mercoledì 23 maggio 12
js.rake file in /lib/tasks your_app/lib/tasks/js.rake # You can generate your routes file by doing the following: rake js:routes # The above commands will place the routes in your_app/public/javascripts/rails_routes.js # You can specify your own filename like so: rake js:routes[custom_name.js] $.ajax({ url: "/post/" + post_id; method: 'PUT', data:{ x: post_x, y: post_y } }); $.ajax({ url: post_path({id: post_id}) method: 'PUT', data:{ x: post_x, y: post_y } }); Broken on Rails >= 3.1 mercoledì 23 maggio 12
en: # you can leave empty locales, for example the default one es: products: productos contact: contacto new: crear products_en GET /products(.:format) {:action=>"index", :controller=>"products"} products_es GET /es/productos(.:format) {:action=>"index", :controller=>"products"} POST /products(.:format) {:action=>"create", :controller=>"products"} POST /es/productos(.:format) {:action=>"create", :controller=>"products"} part I mercoledì 23 maggio 12