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

  • 2,357 views
Uploaded on

2011/6/8 Hamamatsu.rb#4の浜松Rails3道場の資料です。 …

2011/6/8 Hamamatsu.rb#4の浜松Rails3道場の資料です。

https://github.com/hamamatsu-rb/rails3dojo

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,357
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
25
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. RoutingHamamatsurb#4 2011.06.08 @mackato
  • 2. Wiki or CMS 5
  • 3. Routing
  • 4. Version 1.9.2 Version 3.0.7.rvmrcrvm ruby-1.9.2-p180@rails-3_0_7
  • 5. rails new rails3dojo -m http://railswizard.org/ae25c16f22597ad5ea98.rb -J -T
  • 6. ≠ CSS ≠ Erb
  • 7. Gemfilegem rake, 0.8.7 gem uninstall rake -v=0.9.x bundle update ※ Rails3.1 0.9.1 OK!
  • 8. Mac only!cd ~/.powln -s /path/to/rails3dojoopen http://rails3dojo.dev/
  • 9. http://localhost:3000/rm public/index.html public/images/rails.png
  • 10. http://localhost:3000/ Routing
  • 11. 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
  • 12. rails g controller welcome index
  • 13. 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
  • 14. bundle exec rspec spec/controllers/welcome_controller_spec.rb
  • 15. config/routes.rbRails3dojo::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/
  • 16. 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
  • 17. http://localhost:3000/
  • 18. www.getskeleton.com/
  • 19. http://html2haml.heroku.com/
  • 20. rm -f app/views/layouts/application.html.erbgit clone git://gist.github.com/1012788.gitmv 1012788/application.html.haml app/views/layouts/rm -rf 1012788git clone git://gist.github.com/1012756.gitrm -rf 1012756/.gitmv 1012756 public/stylesheets/sass
  • 21. http://localhost:3000/
  • 22. config/routes.rbRails3dojo::Application.routes.draw do  # You can have the root of your site routed with "root"  root :to => "welcome#index"    resources :pagesend
  • 23. % 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"}
  • 24. Collection Member Pages Pageindex 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)
  • 25. rails g controller pages index new
  • 26. http://localhost:3000/pages
  • 27. http://localhost:3000/pages/new
  • 28. spec/controllers/pages_controller_spec.rb# -*- coding: utf-8 -*-require spec_helperdescribe 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  endend
  • 29. 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
  • 30. http://localhost:3000/
  • 31. config/routes.rbRails3dojo::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  endend http://localhost:3000/admin/pages
  • 32. % 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)
  • 33. config/routes.rbRails3dojo::Application.routes.draw do  # You can have the root of your site routed with "root"  root :to => "welcome#index"    resources :pages do    resources :comments  endend http://localhost:3000/pages/2/comments
  • 34. 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)
  • 35. config/routes.rbRails3dojo::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  endend http://localhost:3000/pages/freeze http://localhost:3000/pages/2/preview
  • 36. 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)