views:

134

answers:

2

I have an images table with a nsfw flag.

What's the best way to add nsfw and sfw to the URL?

For example,

if I have an image with an id=1 and nsfw flag is true, I want the URL to be /images/nsfw/1

if I have an image with an id=2 and nsfw flag is false, I want the URL to be /images/sfw/2

+1  A: 

This can be done with routes:

map.connect ':controller/images/:nsfw/:id', :action => 'show', :nsfw => /n?sfw/

You then can check the nsfw values (which will be 'nsfw' or 'sfw') the same way you get :id.

Kathy Van Stone
Is there an argument that I can pass to `map.resources :images` so that when I <%= link_to 'image', image %> it will create the correct n?sfw link?
mculp
+1  A: 

Named routes will do what you want.

map.sfw '/images/sfw/:id', :action => 'show' , :controller => 'images', :params => {:nsfw => false}
map.nsfw '/images/nsfw/:id', :action => 'show' , :controller => 'images', :params => {:nsfw => true}

This will provide handy methods for link_to and route correctly. setting the :nsfw value in the params hash accordingly.

EmFi