Skip to content

Instantly share code, notes, and snippets.

@lucatironi
Created December 2, 2011 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucatironi/1423620 to your computer and use it in GitHub Desktop.
Save lucatironi/1423620 to your computer and use it in GitHub Desktop.
Github's Style Routes
require 'spec_helper'
describe CategoriesController do
describe "routing" do
it '/solutions to Category#index' do
path = categories_path
path.should == '/solutions'
{ :get => path }.should route_to(
:controller => 'categories',
:action => 'index'
)
end
end
end

Readme

This routing allows the creation of urls like:

This implementation will need the use of the friendly_id gem (https://github.com/norman/friendly_id) in order to generate and use the category and service's names as id.

MyApp::Application.routes.draw do
# snip...
scope '/solutions' do
resources :categories, :path => '', :only => [:index] do
resources :services, :path => '', :only => [:index, :show] do
get '/pay', :to => "orders#pay", :as => "pay"
get '/buy', :to => "orders#new", :as => "buy"
post '/buy', :to => "orders#create"
delete '/cancel', :to => "orders#destroy"
end
end
end
end
require 'spec_helper'
describe ServicesController do
describe "routing" do
it '/solutions/:category_id to Services#index' do
path = category_services_path 'foocategory'
path.should == '/solutions/foocategory'
{ :get => path }.should route_to(
:controller => 'services',
:action => 'index',
:category_id => 'foocategory'
)
end
it '/solutions/:category_id/:service_id to Services#show' do
path = category_service_path 'widgets', 'awesome_widget'
path.should == '/solutions/widgets/awesome_widget'
{ :get => path }.should route_to(
:controller => 'services',
:action => 'show',
:category_id => 'widgets',
:id => 'awesome_widget'
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment