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