Skip to content

Instantly share code, notes, and snippets.

@aghull
Created November 24, 2012 19:52
Show Gist options
  • Save aghull/4141190 to your computer and use it in GitHub Desktop.
Save aghull/4141190 to your computer and use it in GitHub Desktop.
routing
# long-hand create routes directly
path 'users' do
get { User.all }
post { User.create params }
var (/\d/) do |id|
get { User.find(id) }
...
end
...
end
# standard REST methods are all defined so we can instead just do:
path 'users' do
get_all
post_new
get_by_id
get 'some_other_method' do
# logic
end
...
end
# these are also collected into common groupings:
path 'users' { rest_resource } # includes 4 CRUD methods
path 'users' { rest_resource_with_forms } # includes the same 7 methods as rails resource method
# or ofc create our own groupings
def collection_resource
get_all
post_new
end
path 'users' { collection_resource }
# custom wrapping blocks
path 'users' do
get_by_id { yield if have_access_to id }
get_paged page_size:10
...
end
# child resources
path 'users' do
rest_resource
var (/\d/) do |id|
path 'posts' do
child_rest_resource # uses existing path components to get the correct :user_id
end
end
end
# -or-
path 'users' do
rest_resource
instance_child_rest_resource 'posts' # establishes routes under ':id/posts/...'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment