RESTful/Resource Routes
By default every route you define in Mack is RESTful. Unless you specify a route, it will default to the HTTP verb GET.
Mack::Routes.build do |r| r.home_page '/', :controller => :default, :action => :index r.users_create '/users', :controller => :users, :action => :create, :method => :post end
In the above example when a request comes in that matches the url '/' and is of type GET then it will be sent to the DefaultController and the index action. If '/' comes in on ANY other HTTP verb, it will not be matched a Mack::Errors::ResourceNotFound error will be raised. The same holds true if a request comes in that matches '/users', if it's a POST then it will go to the UsersController and the create action.
As demonstrated above you can set the HTTP method with :method => :get|:post|:put|:delete
It is very much so possible to 'overload' a url by specifying many routes with different methods.
Mack::Routes.build do |r| r.users_index '/users', :controller => :users, :action => :index r.users_create '/users', :controller => :users, :action => :create, :method => :post end
In this example if '/users' is GET request, then it will be sent to UsersController#index if it's a POST request it will be sent to UsersController#create.
If you want to map a full suite of RESTful routes, you can do this with just one line:
Mack::Routes.build do |r| r.resource :users end
This is the equivalent of mapping the following routes:
Mack::Routes.build do |r| r.users_index '/users', :controller => :users, :action => :index r.users_new '/users/new', :controller => :users, :action => :new r.users_show '/users/:id', :controller => :users, :action => :show r.users_edit '/users/:id/edit', :controller => :users, :action => :edit r.users_create '/users', :controller => :users, :action => :create, :method => :post r.users_update '/users/:id', :controller => :users, :action => :update, :method => :put r.users_delete '/users/:id', :controller => :users, :action => :delete, :method => :delete end
Resource routes in Mack can easily be extended by passing in an optional block, like such:
Mack::Routes.build do |r|
r.resource :users do |user|
user.foo '/users/foo', :action => :foo
user.hello '/hello/:id', :action => :hello, :method => :put
end
end
This is the equivalent of mapping the following routes:
Mack::Routes.build do |r| r.users_foo '/users/foo', :controller => :users, :action => :foo r.users_hello '/users/hello/:id', :controller => :users, :action => :hello, :method => :put r.users_index '/users', :controller => :users, :action => :index r.users_new '/users/new', :controller => :users, :action => :new r.users_show '/users/:id', :controller => :users, :action => :show r.users_edit '/users/:id/edit', :controller => :users, :action => :edit r.users_create '/users', :controller => :users, :action => :create, :method => :post r.users_update '/users/:id', :controller => :users, :action => :update, :method => :put r.users_delete '/users/:id', :controller => :users, :action => :delete, :method => :delete end
There are a couple of things to notice here. One is that the routes defined in the block actually get generated before the routes that normally get generated by the resource method. Also note that the foo and hello named routes actually got namespaced with 'users_'. This helps to tie them to the user resource. Finally, note that the hello route, '/hello/:id', became '/users/hello/:id', again this is to tie it to the the user resource.
The resource method also takes an optional Hash. These options are applied to all the resourced routes, so be careful with them. Here's an example:
Mack::Routes.build do |r| r.resource :users, :controller => :peeps end
Now instead of going to the UsersController requests for the users resource will be sent to the PeepsController.