Rails Routing    and URL design   @christian_mager 10.10.2012 - hiq5.com
Basics         Rails.application.routes.draw do             root to: “welcome#say_hello“         end                      ...
Basics Rails.application.routes.draw do      match products/:id => catalog#view end                                     cl...
BasicsRails.application.routes.draw do  match posts/:year/:month => posts#indexend                        http://localhost...
BasicsRails.application.routes.draw do match products/:id/purchase => catalog#purchase,          as => :purchaseendpurchas...
BasicsRails.application.routes.draw do match products/:id/purchase => catalog#purchase,          via: [ :get, :post ] matc...
Basics Rails.application.routes.draw do     match content/*path => pages#show end                              http://loca...
Basics   Rails.application.routes.draw do      match content/*path => pages#show, format: false   end                     ...
$ rake routes        users GET            /users(.:format)                     users#index              POST           /us...
RESTRepresentationalStateTransfer
REST, the rails wayRails.application.routes.draw do resource :userend
REST, the rails way                                           class UsersController < AppControllerhttp://localhost:3000/u...
REST, the rails wayRails.application.routes.draw do resources :usersend
REST, the rails way                                                class UsersController < AppControllerhttp://localhost:3...
REST, the rails wayRails.application.routes.draw doresources :users, only: [ :show, :new,                   :create, :upda...
REST, the rails way                                          class UsersController < AppControllerhttp://localhost:3000/us...
REST, the rails wayRails.application.routes.draw doresources :users, controller: “Members“resources :members, controller: ...
REST, the rails wayRails.application.routes.draw do resources :users do   get :posts, on: :member endend                  ...
REST, the rails wayRails.application.routes.draw do  resources :users do    member do      get :posts    end  endend      ...
REST, the rails wayRails.application.routes.draw do resources :users do   collection do     get :posts   end end          ...
REST, the rails wayRails.application.routes.draw do  resources :users do    resources :posts  endendhttp://localhost:3000/...
REST, the rails wayhttp://localhost:3000/users/newapp/views/users/new.html.erb                        class UsersControlle...
REST, the rails way http://localhost:3000/user/123/edit app/views/users/edit.html.erb                           class User...
REST, the rails way           starting at Rails 4.0       PUT                  PATCH{ user: {                    { user: {...
NamespacesRails.application.routes.draw do  namespace :admin do    resources :users  endend                               ...
Scopes  Rails.application.routes.draw do    scope "/admin" do      resources :users    end  end                           ...
Scopes  Rails.application.routes.draw do    resources :users, :path => "/admin/users"  end                                ...
Scopes  Rails.application.routes.draw do    scope "(:locale)", locale: /de|en/ do      resources :users    end  end       ...
Thanks :)• cd rails_app  rake routes• http://guides.rubyonrails.org/routing.html
Upcoming SlideShare
Loading in...5
×

Rails Routing and URL design

2,390

Published on

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,390
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Rails Routing and URL design"

    1. 1. Rails Routing and URL design @christian_mager 10.10.2012 - hiq5.com
    2. 2. Basics Rails.application.routes.draw do root to: “welcome#say_hello“ end class WelcomeController < AppControllerremove public/index.html ! def say_hello ... end end
    3. 3. Basics Rails.application.routes.draw do match products/:id => catalog#view end class CatalogController < AppController def viewhttp://localhost:3000/products/123 @product = Product.find(params[:id]) end end
    4. 4. BasicsRails.application.routes.draw do match posts/:year/:month => posts#indexend http://localhost:3000/posts/2012/10 class PostsController < AppController{ params: { year: “2012“, def index month: “10“ @post = Post.where( year: params[:year],}} month: params[:month] ) end end
    5. 5. BasicsRails.application.routes.draw do match products/:id/purchase => catalog#purchase, as => :purchaseendpurchase_url(:id => product.id) http://localhost:3000/products/123/purchase
    6. 6. BasicsRails.application.routes.draw do match products/:id/purchase => catalog#purchase, via: [ :get, :post ] match products/:id/purchase => catalog#update_purchase, via: [ :put ]end
    7. 7. Basics Rails.application.routes.draw do match content/*path => pages#show end http://localhost:3000/content/path/to/imprint.html{ params: { class PagesController < AppController path: “path/to/imprint“, format: “html“ def show}} @page = Page.find_by_path(params[:path]) end end
    8. 8. Basics Rails.application.routes.draw do match content/*path => pages#show, format: false end http://localhost:3000/content/path/to/imprint.html class PagesController < AppController{ params: { path: “path/to/imprint.html“ def show}} @page = Page.find_by_path(params[:path]) end end
    9. 9. $ rake routes users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy root / welcome#say_helloin views: in controllers:<%= form_for @user, url: user_path(@user) do |f| %> class UsersController < AppController ... def update<% end %> ... redirect_to user_url(@user) return end end
    10. 10. RESTRepresentationalStateTransfer
    11. 11. REST, the rails wayRails.application.routes.draw do resource :userend
    12. 12. REST, the rails way class UsersController < AppControllerhttp://localhost:3000/user def showuser_url() GET endhttp://localhost:3000/user/new def newnew_user_url() GET endhttp://localhost:3000/user def createuser_url() POST endhttp://localhost:3000/user/edit def editedit_user_url() GET endhttp://localhost:3000/user def updateuser_url() PUT endhttp://localhost:3000/user def destroyuser_url() DELETE end end
    13. 13. REST, the rails wayRails.application.routes.draw do resources :usersend
    14. 14. REST, the rails way class UsersController < AppControllerhttp://localhost:3000/users def indexusers_url() GET endhttp://localhost:3000/users/123 def showuser_url(@user) GET end def newhttp://localhost:3000/users/newnew_user_url() GET endhttp://localhost:3000/users def createusers_url() POST end def edithttp://localhost:3000/users/123/editedit_user_url(@user) GET endhttp://localhost:3000/users/123 def updateuser_url(@user) PUT endhttp://localhost:3000/users/123 def destroyuser_url(@user) DELETE end end
    15. 15. REST, the rails wayRails.application.routes.draw doresources :users, only: [ :show, :new, :create, :update, :destroy ]resources :users, except: [ :index, :edit ]end
    16. 16. REST, the rails way class UsersController < AppControllerhttp://localhost:3000/user/123 def showuser_url(@user) GET end def newhttp://localhost:3000/user/newnew_user_url(@user) GET endhttp://localhost:3000/users def createusers_url() POST endhttp://localhost:3000/user/123 def updateuser_url(@user) PUT endhttp://localhost:3000/user/123 def destroyuser_url(@user) DELETE end end
    17. 17. REST, the rails wayRails.application.routes.draw doresources :users, controller: “Members“resources :members, controller: “Users“, as: :peopleend
    18. 18. REST, the rails wayRails.application.routes.draw do resources :users do get :posts, on: :member endend class UsersController < AppController ... def postshttp://localhost:3000/users/123/posts @posts = User.find(params[:id]).postsuser_posts_url(@user) end end
    19. 19. REST, the rails wayRails.application.routes.draw do resources :users do member do get :posts end endend class UsersController < AppController ... def posts @posts = User.find(params[:id]).postshttp://localhost:3000/users/123/posts enduser_posts_url(@user) end
    20. 20. REST, the rails wayRails.application.routes.draw do resources :users do collection do get :posts end end class UsersController < AppControllerend ... def groupshttp://localhost:3000/users/groups endgroups_users_url() end
    21. 21. REST, the rails wayRails.application.routes.draw do resources :users do resources :posts endendhttp://localhost:3000/users/123 http://localhost:3000/users/123/posts/5user_url(@user) user_post_url(@user, @post)class UsersController < AppController class PostsController < AppController ... ... def show def show @user = User.find(params[:id]) @user = User.find(params[:user_id]) end @post = @user.posts.find(params[:id])end end end
    22. 22. REST, the rails wayhttp://localhost:3000/users/newapp/views/users/new.html.erb class UsersController < AppController<%= form_for @user do |f| %> ... <%= f.text_field :first_name %> ... def new<% end %> @user = User.new end def create @user = User.new params[:user] if @user.save<form action=“/users“ method=“post“> redirect_to user_url(@user) <div style="margin:0;padding:0;display:inline"> return ... else </div> render :new end <input name="user[first_name]" type="text" /> ... end</form> ... end
    23. 23. REST, the rails way http://localhost:3000/user/123/edit app/views/users/edit.html.erb class UsersController < AppController <%= form_for @user do |f| %> ... <%= f.text_field :first_name %> ... def edit <% end %> @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) redirect_to user_url(@user)<form action=“/users/123“ method=“post“> return <div style="margin:0;padding:0;display:inline"> ... else <input type=“hidden“ name=“_method“ value=“put“ /> render :edit </div> end end <input name="user[first_name]" type="text" /> ... ...</form> end
    24. 24. REST, the rails way starting at Rails 4.0 PUT PATCH{ user: { { user: { id: 321, id: 123, first_name: “Jenny“, first_name: “John“, ... ...}} }}
    25. 25. NamespacesRails.application.routes.draw do namespace :admin do resources :users endend app/views/admin/users/ app/controllers/admin/users_controller.rb class Admin::UsersController < AppController ...http://localhost:3000/admin/users/123 endadmin_user_url(@user) module Adminhttp://localhost:3000/admin/users/123/editedit_admin_user_url(@user) class UsersController < ::AppController ... end end
    26. 26. Scopes Rails.application.routes.draw do scope "/admin" do resources :users end end class UsersController < AppControllerhttp://localhost:3000/admin/users/123user_url(@user) ... def show @user = User.find(params[:id]) end end
    27. 27. Scopes Rails.application.routes.draw do resources :users, :path => "/admin/users" end class UsersController < AppControllerhttp://localhost:3000/admin/users/123user_url(@user) ... def show @user = User.find(params[:id]) end end
    28. 28. Scopes Rails.application.routes.draw do scope "(:locale)", locale: /de|en/ do resources :users end end class UsersController < AppControllerhttp://localhost:3000/de/users/123user_url(@user) ... def show{ params: { @user = User.find(params[:id]) locale: “de“ end}} end
    29. 29. Thanks :)• cd rails_app rake routes• http://guides.rubyonrails.org/routing.html

    ×