SlideShare a Scribd company logo
1 of 29
Rails Routing
    and URL design

   @christian_mager
 10.10.2012 - hiq5.com
Basics

         Rails.application.routes.draw do

             root to: “welcome#say_hello“

         end

                             class WelcomeController < AppController

remove public/index.html !     def say_hello
                                 ...
                               end

                             end
Basics

 Rails.application.routes.draw do

      match 'products/:id' => 'catalog#view'

 end

                                     class CatalogController < AppController

                                       def view
http://localhost:3000/products/123       @product = Product.find(params[:id])
                                       end

                                     end
Basics

Rails.application.routes.draw do

  match 'posts/:year/:month' => 'posts#index'

end


                        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
Basics
Rails.application.routes.draw do

 match 'products/:id/purchase' => 'catalog#purchase',
          as => :purchase


end

purchase_url(:id => product.id)     http://localhost:3000/products/123/purchase
Basics
Rails.application.routes.draw do

 match 'products/:id/purchase' => 'catalog#purchase',
          via: [ :get, :post ]

 match 'products/:id/purchase' => 'catalog#update_purchase',
          via: [ :put ]


end
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
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
$ 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_hello


in 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
REST
Representational

State

Transfer
REST, the rails way

Rails.application.routes.draw do

 resource :user

end
REST, the rails way
                                           class UsersController < AppController
http://localhost:3000/user                   def show
user_url()
                                   GET       end


http://localhost:3000/user/new               def new
new_user_url()                     GET       end


http://localhost:3000/user                   def create
user_url()
                                   POST      end


http://localhost:3000/user/edit              def edit
edit_user_url()
                                   GET       end


http://localhost:3000/user                   def update
user_url()
                                   PUT       end


http://localhost:3000/user                   def destroy
user_url()
                                  DELETE
                                             end
                                           end
REST, the rails way

Rails.application.routes.draw do

 resources :users

end
REST, the rails way
                                                class UsersController < AppController
http://localhost:3000/users                       def index
users_url()                             GET       end


http://localhost:3000/users/123                   def show
user_url(@user)                         GET       end


                                                  def new
http://localhost:3000/users/new
new_user_url()
                                        GET       end



http://localhost:3000/users                       def create
users_url()
                                        POST      end


                                                  def edit
http://localhost:3000/users/123/edit
edit_user_url(@user)
                                        GET       end


http://localhost:3000/users/123                   def update
user_url(@user)
                                        PUT       end


http://localhost:3000/users/123                   def destroy
user_url(@user)
                                       DELETE     end
                                                end
REST, the rails way

Rails.application.routes.draw do

resources :users, only: [ :show, :new,
                   :create, :update, :destroy ]


resources :users, except: [ :index, :edit ]

end
REST, the rails way
                                          class UsersController < AppController
http://localhost:3000/user/123              def show
user_url(@user)                   GET       end


                                            def new
http://localhost:3000/user/new
new_user_url(@user)
                                  GET       end



http://localhost:3000/users                 def create
users_url()
                                  POST      end


http://localhost:3000/user/123              def update
user_url(@user)
                                  PUT       end


http://localhost:3000/user/123              def destroy
user_url(@user)
                                 DELETE     end
                                          end
REST, the rails way
Rails.application.routes.draw do

resources :users, controller: “Members“

resources :members, controller: “Users“,
                            as: :people

end
REST, the rails way
Rails.application.routes.draw do

 resources :users do
   get :posts, on: :member
 end

end
                                        class UsersController < AppController
                                          ...
                                          def posts
http://localhost:3000/users/123/posts
                                            @posts = User.find(params[:id]).posts
user_posts_url(@user)
                                          end

                                        end
REST, the rails way
Rails.application.routes.draw do

  resources :users do
    member do
      get :posts
    end
  end

end                                     class UsersController < AppController
                                          ...
                                          def posts
                                            @posts = User.find(params[:id]).posts
http://localhost:3000/users/123/posts     end
user_posts_url(@user)
                                        end
REST, the rails way
Rails.application.routes.draw do

 resources :users do
   collection do
     get :posts
   end
 end

                                     class UsersController < AppController
end                                    ...
                                       def groups

http://localhost:3000/users/groups     end
groups_users_url()
                                     end
REST, the rails way
Rails.application.routes.draw do

  resources :users do
    resources :posts
  end

end


http://localhost:3000/users/123         http://localhost:3000/users/123/posts/5
user_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
REST, the rails way
http://localhost:3000/users/new
app/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
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
REST, the rails way
           starting at Rails 4.0

       PUT                  PATCH

{ user: {                    { user: {
   id: 321,                     id: 123,
   first_name: “Jenny“,         first_name: “John“,
   ...                          ...
}}                           }}
Namespaces
Rails.application.routes.draw do

  namespace :admin do
    resources :users
  end

end                                          app/views/admin/users/
                                             app/controllers/admin/users_controller.rb
                                             class Admin::UsersController < AppController
                                               ...
http://localhost:3000/admin/users/123        end
admin_user_url(@user)
                                             module Admin
http://localhost:3000/admin/users/123/edit
edit_admin_user_url(@user)
                                               class UsersController < ::AppController
                                                 ...
                                               end
                                             end
Scopes
  Rails.application.routes.draw do

    scope "/admin" do
      resources :users
    end

  end


                                         class UsersController < AppController
http://localhost:3000/admin/users/123
user_url(@user)                            ...
                                           def show
                                             @user = User.find(params[:id])
                                           end
                                         end
Scopes
  Rails.application.routes.draw do

    resources :users, :path => "/admin/users"

  end



                                         class UsersController < AppController
http://localhost:3000/admin/users/123
user_url(@user)                            ...
                                           def show
                                             @user = User.find(params[:id])
                                           end
                                         end
Scopes
  Rails.application.routes.draw do

    scope "(:locale)", locale: /de|en/ do
      resources :users
    end

  end


                                      class UsersController < AppController
http://localhost:3000/de/users/123
user_url(@user)                         ...
                                        def show
{ params: {                               @user = User.find(params[:id])
   locale: “de“                         end
}}                                    end
Thanks :)


• cd rails_app
  rake routes

• http://guides.rubyonrails.org/routing.html

More Related Content

What's hot

Feature flagging with rails engines
Feature flagging with rails enginesFeature flagging with rails engines
Feature flagging with rails enginesEnrico Teotti
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful RailsBen Scofield
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriAbdul Malik Ikhsan
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsMark
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowVrann Tulika
 
Using and reusing CakePHP plugins
Using and reusing CakePHP pluginsUsing and reusing CakePHP plugins
Using and reusing CakePHP pluginsPierre MARTIN
 
Chef 0.8, Knife and Amazon EC2
Chef 0.8, Knife and Amazon EC2Chef 0.8, Knife and Amazon EC2
Chef 0.8, Knife and Amazon EC2Robert Berger
 
Spring-training-in-bangalore
Spring-training-in-bangaloreSpring-training-in-bangalore
Spring-training-in-bangaloreTIB Academy
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Arun Gupta
 
2007 Fsoss Drupal Under The Hood
2007 Fsoss Drupal Under The Hood2007 Fsoss Drupal Under The Hood
2007 Fsoss Drupal Under The HoodJames Walker
 

What's hot (13)

Feature flagging with rails engines
Feature flagging with rails enginesFeature flagging with rails engines
Feature flagging with rails engines
 
Rails3 changesets
Rails3 changesetsRails3 changesets
Rails3 changesets
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
About REST & Symfony
About REST & SymfonyAbout REST & Symfony
About REST & Symfony
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate Uri
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.js
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 
Using and reusing CakePHP plugins
Using and reusing CakePHP pluginsUsing and reusing CakePHP plugins
Using and reusing CakePHP plugins
 
Chef 0.8, Knife and Amazon EC2
Chef 0.8, Knife and Amazon EC2Chef 0.8, Knife and Amazon EC2
Chef 0.8, Knife and Amazon EC2
 
Spring-training-in-bangalore
Spring-training-in-bangaloreSpring-training-in-bangalore
Spring-training-in-bangalore
 
DRUPAL Menu System
DRUPAL Menu SystemDRUPAL Menu System
DRUPAL Menu System
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
 
2007 Fsoss Drupal Under The Hood
2007 Fsoss Drupal Under The Hood2007 Fsoss Drupal Under The Hood
2007 Fsoss Drupal Under The Hood
 

Viewers also liked

Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2RORLAB
 
20120121 rbc rails_routing
20120121 rbc rails_routing20120121 rbc rails_routing
20120121 rbc rails_routingTakeshi AKIMA
 
Rails Routing And Rendering
Rails Routing And RenderingRails Routing And Rendering
Rails Routing And RenderingJames Gray
 
Rails training presentation routing
Rails training presentation   routingRails training presentation   routing
Rails training presentation routingtheacadian
 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1RORLAB
 

Viewers also liked (6)

Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
 
20120121 rbc rails_routing
20120121 rbc rails_routing20120121 rbc rails_routing
20120121 rbc rails_routing
 
Rails Routing And Rendering
Rails Routing And RenderingRails Routing And Rendering
Rails Routing And Rendering
 
Rails training presentation routing
Rails training presentation   routingRails training presentation   routing
Rails training presentation routing
 
Routing
RoutingRouting
Routing
 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1
 

Similar to Rails Routing and URL design

More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2pyjonromero
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful RailsViget Labs
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Umair Amjad
 
Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Joao Lucas Santana
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4shnikola
 
Como programar un blog REST
Como programar un blog RESTComo programar un blog REST
Como programar un blog RESTJavier Vidal
 
I Phone On Rails
I Phone On RailsI Phone On Rails
I Phone On RailsJohn Wilker
 
Binary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel ControllersBinary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel ControllersBinary Studio
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuLucas Renan
 
Emberjs as a rails_developer
Emberjs as a rails_developer Emberjs as a rails_developer
Emberjs as a rails_developer Sameera Gayan
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010Plataformatec
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful CodeGreggPollack
 

Similar to Rails Routing and URL design (20)

More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Rails introduction
Rails introductionRails introduction
Rails introduction
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4
 
Como programar un blog REST
Como programar un blog RESTComo programar un blog REST
Como programar un blog REST
 
I Phone On Rails
I Phone On RailsI Phone On Rails
I Phone On Rails
 
Rest And Rails
Rest And RailsRest And Rails
Rest And Rails
 
Binary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel ControllersBinary Studio Academy 2016: Laravel Controllers
Binary Studio Academy 2016: Laravel Controllers
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
 
Emberjs as a rails_developer
Emberjs as a rails_developer Emberjs as a rails_developer
Emberjs as a rails_developer
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010Rails 3 - The Developers Conference - 21aug2010
Rails 3 - The Developers Conference - 21aug2010
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful Code
 

Rails Routing and URL design

  • 1. Rails Routing and URL design @christian_mager 10.10.2012 - hiq5.com
  • 2. Basics Rails.application.routes.draw do root to: “welcome#say_hello“ end class WelcomeController < AppController remove public/index.html ! def say_hello ... end end
  • 3. Basics Rails.application.routes.draw do match 'products/:id' => 'catalog#view' end class CatalogController < AppController def view http://localhost:3000/products/123 @product = Product.find(params[:id]) end end
  • 4. Basics Rails.application.routes.draw do match 'posts/:year/:month' => 'posts#index' end 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. Basics Rails.application.routes.draw do match 'products/:id/purchase' => 'catalog#purchase', as => :purchase end purchase_url(:id => product.id) http://localhost:3000/products/123/purchase
  • 6. Basics Rails.application.routes.draw do match 'products/:id/purchase' => 'catalog#purchase', via: [ :get, :post ] match 'products/:id/purchase' => 'catalog#update_purchase', via: [ :put ] end
  • 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. 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. $ 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_hello in 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
  • 11. REST, the rails way Rails.application.routes.draw do resource :user end
  • 12. REST, the rails way class UsersController < AppController http://localhost:3000/user def show user_url() GET end http://localhost:3000/user/new def new new_user_url() GET end http://localhost:3000/user def create user_url() POST end http://localhost:3000/user/edit def edit edit_user_url() GET end http://localhost:3000/user def update user_url() PUT end http://localhost:3000/user def destroy user_url() DELETE end end
  • 13. REST, the rails way Rails.application.routes.draw do resources :users end
  • 14. REST, the rails way class UsersController < AppController http://localhost:3000/users def index users_url() GET end http://localhost:3000/users/123 def show user_url(@user) GET end def new http://localhost:3000/users/new new_user_url() GET end http://localhost:3000/users def create users_url() POST end def edit http://localhost:3000/users/123/edit edit_user_url(@user) GET end http://localhost:3000/users/123 def update user_url(@user) PUT end http://localhost:3000/users/123 def destroy user_url(@user) DELETE end end
  • 15. REST, the rails way Rails.application.routes.draw do resources :users, only: [ :show, :new, :create, :update, :destroy ] resources :users, except: [ :index, :edit ] end
  • 16. REST, the rails way class UsersController < AppController http://localhost:3000/user/123 def show user_url(@user) GET end def new http://localhost:3000/user/new new_user_url(@user) GET end http://localhost:3000/users def create users_url() POST end http://localhost:3000/user/123 def update user_url(@user) PUT end http://localhost:3000/user/123 def destroy user_url(@user) DELETE end end
  • 17. REST, the rails way Rails.application.routes.draw do resources :users, controller: “Members“ resources :members, controller: “Users“, as: :people end
  • 18. REST, the rails way Rails.application.routes.draw do resources :users do get :posts, on: :member end end class UsersController < AppController ... def posts http://localhost:3000/users/123/posts @posts = User.find(params[:id]).posts user_posts_url(@user) end end
  • 19. REST, the rails way Rails.application.routes.draw do resources :users do member do get :posts end end end class UsersController < AppController ... def posts @posts = User.find(params[:id]).posts http://localhost:3000/users/123/posts end user_posts_url(@user) end
  • 20. REST, the rails way Rails.application.routes.draw do resources :users do collection do get :posts end end class UsersController < AppController end ... def groups http://localhost:3000/users/groups end groups_users_url() end
  • 21. REST, the rails way Rails.application.routes.draw do resources :users do resources :posts end end http://localhost:3000/users/123 http://localhost:3000/users/123/posts/5 user_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. REST, the rails way http://localhost:3000/users/new app/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. 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. REST, the rails way starting at Rails 4.0 PUT PATCH { user: { { user: { id: 321, id: 123, first_name: “Jenny“, first_name: “John“, ... ... }} }}
  • 25. Namespaces Rails.application.routes.draw do namespace :admin do resources :users end end app/views/admin/users/ app/controllers/admin/users_controller.rb class Admin::UsersController < AppController ... http://localhost:3000/admin/users/123 end admin_user_url(@user) module Admin http://localhost:3000/admin/users/123/edit edit_admin_user_url(@user) class UsersController < ::AppController ... end end
  • 26. Scopes Rails.application.routes.draw do scope "/admin" do resources :users end end class UsersController < AppController http://localhost:3000/admin/users/123 user_url(@user) ... def show @user = User.find(params[:id]) end end
  • 27. Scopes Rails.application.routes.draw do resources :users, :path => "/admin/users" end class UsersController < AppController http://localhost:3000/admin/users/123 user_url(@user) ... def show @user = User.find(params[:id]) end end
  • 28. Scopes Rails.application.routes.draw do scope "(:locale)", locale: /de|en/ do resources :users end end class UsersController < AppController http://localhost:3000/de/users/123 user_url(@user) ... def show { params: { @user = User.find(params[:id]) locale: “de“ end }} end
  • 29. Thanks :) • cd rails_app rake routes • http://guides.rubyonrails.org/routing.html

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n