RESTful Routes Demystified

SEP

14

2008

0 comments

RESTful application design is a rails-standard way to structure your CRUD actions. It simplifies your controllers into 7 actions: index, show, new, create, edit, update, and destroy. Here are some basics for working with RESTful routes.

The simple way to get the default routes:

ActionController::Routing::Routes.draw do |map|
  map.resources :products
end

Route Options

Several options can be passed to the route to customize it. You can use the path_prefix option to customize the appearance of your URLs. The name_prefix option is used to change the way the routes are called. The controller option is used to specify a custom controller.

# start all of your URLs with /admin
# '/admin/projects/:id/edit'
map.resources :products, :path_prefix => '/admin'

# start all of your routes with 'mng_'
# mng_products_path or mng_product_path(:id)
map.resources :products, :name_prefix => 'mng_'

# if you want your URLs to say '/products/:id'
# but your controller is called 'store_products'
map.resources :products, :controller => 'store_products'

Adding Routes for Custom Actions

The resources method will generate the routes for the default REST actions, but what if you want to add your own actions to your controller? There are two options for adding custom routes: the member option is for routes that require an id to be passed, and the collection option is for actions that don't require an id. With each option, you must pass a hash with the action name and the HTTP method.

# return_policy_products_path => '/products/return_policy'
# submit_question_products_path => '/products/submit_question'
map.resources :products, :collection => {:return_policy => :get, :submit_question => :post}

# warranty_product_path(:id) => '/products/:id/warranty'
map.resources :products, :member => {:warranty => :get}

Nested Routes

You can nest routes for objects that are related. Say the products have customer reviews with a has_many relationship. You can nest the resources call within the products route definition.

# product_reviews_path(:product_id) => '/products/:product_id/reviews'
# product_review_path(:product_id, :id) => '/products/:product_id/reviews/:id'
# edit_product_review_path(:product_id, :id) => '/products/:product_id/reviews/:id/edit'
map.resources :products do |products|
  products.resources :reviews, :controller => 'customer_reviews'
end

Those are the basics, but there are a LOT more possibilities. If you're interested, there is a really great guide to all things routes.

Tagged: tutorial, routes, rails

Code Golf: Seven-Segment Displays

SEP

08

2008

0 comments

Here is my submission for the Seven-Segment Displays challenge. Code size: 292 bytes.

x=gets.chop.split ''
a=' ### '
b='#    '
c='    #'
d='#   #'
e=' '*5
f='  '
g=[a]
h=[d]*3
i=[e]
j=[c]*3
k=[b]*3
l=g+h
m=g+j
n=m+g
y={0=>l+i+h+g,1=>i+j+i+j+i,2=>n+k+g,3=>m+n,4=>i+h+m+i,5=>g+k+n,6=>g+k+l+g,7=>m+i+j+i,8=>l+l+g,9=>l+n}
9.times{|i|puts x.collect{|n|y[n.to_i][i]+f}.join.chop.chop}

Tagged: code golf, ruby

Code Golf: Bob Ross' The Joy of ASCII Art

SEP

01

2008

0 comments

Here is my submission for the Bob Ross' The Joy of ASCII Art challenge. Code size: 116 bytes.

a=(['']*39).map{[' ']*79}
while gets
x=split
a[x[1].to_i][x[0].to_i]=x[2].to_i.chr
end
a.each{|x|puts x.join.rstrip}

Tagged: ruby, code golf

Code Golf: Grid Computing

AUG

31

2008

0 comments

Here is my submission for the Grid Computing challenge. Code size: 135 bytes.

a=[]
10.times{|x|a<<gets.split}
y=[0]*20
10.times{|i|a.each{|x|y[i]+=x[i].to_i}}
a.each{|x|x.each{|i|y[a.index(x)+10]+=i.to_i}}
p y.max

Tagged: ruby, code golf

Code Golf: Saving Time

AUG

28

2008

1 comment

Here is my submission for the Saving Time challenge. Code size: 319 bytes (not the worst, but close).

h,m=gets.split(':').map{|i|i.to_i}
h>11?h-=12:''
m=(m-m%5)/5
a=[]
11.times{|x|a<<[' ']*17}
b={}
c=[[0,8],[1,12],[3,15],[5,16],[7,15],[9,12],[10,8],[9,4],[7,1],[5,0],[3,1],[1,4]]
0.upto(11){|x|b[x]=c[x]}
b.map{|x,n|a[n[0]][n[1]]='o'}
a[b[h][0]][b[h][1]]='h'
a[b[m][0]][b[m][1]]=h==m ?'x':'m'
a.map{|x|puts x.join.rstrip}

Tagged: ruby, code golf


© 2010 Travis Roberts. All rights reserved.