12. HTTP
Methods: GET, POST, PUT, DELETE
Paths: /login, /pages/2, /items.xml
params[:id] format
: Rails Routing from the Outside In. http://guides.rubyonrails.org/routing.html
14. spec/controllers/welcome_controller_spec.rb
# -*- coding: utf-8 -*-
require 'spec_helper'
describe WelcomeController do
describe "#index" do
it "should be successful" do
get 'index'
response.should be_success
end
it "GET / " do
{ :get => "/" }.should route_to(
:controller => "welcome",
:action => "index" )
end
end
end
18. spec/controllers/welcome_controller_spec.rb
# -*- coding: utf-8 -*-
require 'spec_helper'
describe WelcomeController do
describe "#index" do
it "should be successful" do
get 'index'
response.should be_success
end
it "GET root_path " do
{ :get => root_path }.should route_to(
:controller => "welcome",
:action => "index" )
end
end
end
25. % rake routes
root /(.:format) {:controller=>"welcome", :action=>"index"}
pages GET /pages(.:format) {:action=>"index", :controller=>"pages"}
POST /pages(.:format) {:action=>"create", :controller=>"pages"}
new_page GET /pages/new(.:format) {:action=>"new", :controller=>"pages"}
edit_page GET /pages/:id/edit(.:format) {:action=>"edit", :controller=>"pages"}
page GET /pages/:id(.:format) {:action=>"show", :controller=>"pages"}
PUT /pages/:id(.:format) {:action=>"update", :controller=>"pages"}
DELETE /pages/:id(.:format) {:action=>"destroy", :controller=>"pages"}
26. Collection Member
Pages Page
index GET pages_path show GET page_path(id)
new GET new_page_path edit GET edit_page_path(id)
create POST pages_path update PUT page_path(id)
destroy DELETE page_path(id)
30. spec/controllers/pages_controller_spec.rb
# -*- coding: utf-8 -*-
require 'spec_helper'
describe PagesController do
describe "#index'" do
...
it "GET pages_path " do
{ :get => pages_path }.should route_to(:controller => "pages", :action => "index" )
end
end
describe "#new'" do
...
it "GET new_page_path " do
{ :get => new_page_path }.should route_to(:controller => "pages", :action => "new" )
end
end
end
38. rake routes
...
page_comments GET /pages/:page_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /pages/:page_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_page_comment GET /pages/:page_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_page_comment GET /pages/:page_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
page_comment GET /pages/:page_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /pages/:page_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /pages/:page_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
comments_path page_comments_path(page_id)
edit_comment_path(id) edit_page_comment_path(page_id, id)
39. config/routes.rb
Rails3dojo::Application.routes.draw do
# You can have the root of your site routed with "root"
root :to => "welcome#index"
resources :pages do
get 'freeze', :on => :collection
get 'preview', :on => :member
end
end
http://localhost:3000/pages/freeze
http://localhost:3000/pages/2/preview
40. rake routes
(in /Users/kato/sandbox/rails3dojo)
root /(.:format) {:controller=>"welcome", :action=>"index"}
freeze_pages GET /pages/freeze(.:format) {:action=>"freeze", :controller=>"pages"}
preview_page GET /pages/:id/preview(.:format) {:action=>"preview", :controller=>"pages"}
pages GET /pages(.:format) {:action=>"index", :controller=>"pages"}
POST /pages(.:format) {:action=>"create", :controller=>"pages"}
new_page GET /pages/new(.:format) {:action=>"new", :controller=>"pages"}
edit_page GET /pages/:id/edit(.:format) {:action=>"edit", :controller=>"pages"}
page GET /pages/:id(.:format) {:action=>"show", :controller=>"pages"}
PUT /pages/:id(.:format) {:action=>"update", :controller=>"pages"}
DELETE /pages/:id(.:format) {:action=>"destroy", :controller=>"pages"}
freeze_pages_path
preview_page_path(id)