Routing
Hamamatsurb#4 2011.06.08 @mackato
Wiki or CMS
    5
Routing
Version 1.9.2

             Version 3.0.7

.rvmrc
rvm ruby-1.9.2-p180@rails-3_0_7
rails new rails3dojo -m http://railswizard.org/ae25c16f22597ad5ea98.rb -J -T
≠ CSS   ≠ Erb
Gemfile
gem 'rake', '0.8.7'




    gem uninstall rake -v=0.9.x
    bundle update
          ※ Rails3.1   0.9.1   OK!
Mac only!




cd ~/.pow
ln -s /path/to/rails3dojo
open http://rails3dojo.dev/
http://localhost:3000/




rm public/index.html public/images/rails.png
http://localhost:3000/




     Routing
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
rails g controller welcome index
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
bundle exec rspec 
spec/controllers/welcome_controller_spec.rb
config/routes.rb
Rails3dojo::Application.routes.draw do
  # You can have the root of your site routed with "root"
  root :to => "welcome#index"
end



                      WelcomeController#index
 root_path => /
 root_url => http://localhost:3000/
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
http://localhost:3000/
www.getskeleton.com/
http://html2haml.heroku.com/
rm -f app/views/layouts/application.html.erb
git clone git://gist.github.com/1012788.git
mv 1012788/application.html.haml app/views/layouts/
rm -rf 1012788

git clone git://gist.github.com/1012756.git
rm -rf 1012756/.git
mv 1012756 public/stylesheets/sass
http://localhost:3000/
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
end
% 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"}
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)
rails g controller pages index new
http://localhost:3000/pages
http://localhost:3000/pages/new
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
app/views/layouts/application.html.haml
<!doctype html>
...
        %h3 Shortcuts
        %ul
          %li= link_to "Home", root_path
          %li= link_to "Pages", pages_path
          %li= link_to "New Page", new_page_path
http://localhost:3000/
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
  
  namespace "admin" do
    resources :pages
  end
end




        http://localhost:3000/admin/pages
% rake routes
...
    admin_pages GET      /admin/pages(.:format)            {:action=>"index", :controller=>"admin/pages"}
                POST     /admin/pages(.:format)            {:action=>"create", :controller=>"admin/pages"}
 new_admin_page GET      /admin/pages/new(.:format)        {:action=>"new", :controller=>"admin/pages"}
edit_admin_page GET      /admin/pages/:id/edit(.:format)   {:action=>"edit", :controller=>"admin/pages"}
     admin_page GET      /admin/pages/:id(.:format)        {:action=>"show", :controller=>"admin/pages"}
                PUT      /admin/pages/:id(.:format)        {:action=>"update", :controller=>"admin/pages"}
                DELETE   /admin/pages/:id(.:format)        {:action=>"destroy", :controller=>"admin/pages"}




           pages_path                                            admin_pages_path
           edit_page_path(id)                                    edit_admin_page_path(id)
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
    resources :comments
  end
end




    http://localhost:3000/pages/2/comments
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)
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
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)
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編

浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編

  • 1.
  • 3.
  • 4.
  • 5.
    Version 1.9.2 Version 3.0.7 .rvmrc rvm ruby-1.9.2-p180@rails-3_0_7
  • 6.
    rails new rails3dojo-m http://railswizard.org/ae25c16f22597ad5ea98.rb -J -T
  • 7.
    ≠ CSS ≠ Erb
  • 8.
    Gemfile gem 'rake', '0.8.7' gem uninstall rake -v=0.9.x bundle update ※ Rails3.1 0.9.1 OK!
  • 9.
    Mac only! cd ~/.pow ln-s /path/to/rails3dojo open http://rails3dojo.dev/
  • 10.
  • 11.
  • 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
  • 13.
    rails g controllerwelcome index
  • 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
  • 15.
    bundle exec rspec spec/controllers/welcome_controller_spec.rb
  • 16.
    config/routes.rb Rails3dojo::Application.routes.draw do   # Youcan have the root of your site routed with "root"   root :to => "welcome#index" end WelcomeController#index root_path => / root_url => http://localhost:3000/
  • 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
  • 19.
  • 20.
  • 21.
  • 22.
    rm -f app/views/layouts/application.html.erb gitclone git://gist.github.com/1012788.git mv 1012788/application.html.haml app/views/layouts/ rm -rf 1012788 git clone git://gist.github.com/1012756.git rm -rf 1012756/.git mv 1012756 public/stylesheets/sass
  • 23.
  • 24.
    config/routes.rb Rails3dojo::Application.routes.draw do   # Youcan have the root of your site routed with "root"   root :to => "welcome#index"      resources :pages 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)
  • 27.
    rails g controllerpages index new
  • 28.
  • 29.
  • 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
  • 31.
    app/views/layouts/application.html.haml <!doctype html> ...         %h3 Shortcuts         %ul           %li=link_to "Home", root_path           %li= link_to "Pages", pages_path           %li= link_to "New Page", new_page_path
  • 32.
  • 35.
    config/routes.rb Rails3dojo::Application.routes.draw do   # Youcan have the root of your site routed with "root"   root :to => "welcome#index"      resources :pages      namespace "admin" do     resources :pages   end end http://localhost:3000/admin/pages
  • 36.
    % rake routes ... admin_pages GET /admin/pages(.:format) {:action=>"index", :controller=>"admin/pages"} POST /admin/pages(.:format) {:action=>"create", :controller=>"admin/pages"} new_admin_page GET /admin/pages/new(.:format) {:action=>"new", :controller=>"admin/pages"} edit_admin_page GET /admin/pages/:id/edit(.:format) {:action=>"edit", :controller=>"admin/pages"} admin_page GET /admin/pages/:id(.:format) {:action=>"show", :controller=>"admin/pages"} PUT /admin/pages/:id(.:format) {:action=>"update", :controller=>"admin/pages"} DELETE /admin/pages/:id(.:format) {:action=>"destroy", :controller=>"admin/pages"} pages_path admin_pages_path edit_page_path(id) edit_admin_page_path(id)
  • 37.
    config/routes.rb Rails3dojo::Application.routes.draw do   # Youcan have the root of your site routed with "root"   root :to => "welcome#index"      resources :pages do     resources :comments   end end http://localhost:3000/pages/2/comments
  • 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   # Youcan 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)