# Custom resources

**URL:** <https://www.ruby-forum.com/t/custom-resources/140606>\
**Category:** Rails\
**Created:** [June 16, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/custom-resources/140606 "2008-06-16T19:12:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [June 16, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/custom-resources/140606/1 "2008-06-16T19:12:21Z")

</div>

Hi, i’m curently designing an app which requires lots (and lots) of  
named routes. I want it RESTful, but with paths not named after  
controller. Example of what I need:  
for controller User generate. user\_path, user\_url,  
formatted\_user\_path … etc, but the paths would be specified by me.  
Like user\_path #=\> /our\_customer/:id, where our\_customer is the custom  
thing.  
At first I just did LOTS of named routes, but nicely formatted it took  
about 30 lines per controller and it didnt include formatted\_paths. So  
I refactored it into function, which is about 35 lines long, and bit  
ugly. Here it comes.

8 def custom\_resources mapper, options = {}  
9  
10 controller = options[:controller]  
11 plural = options[:objects] ||  
options[:object].pluralize  
12 singular = options[:object] || plural.singularize  
13 edit\_string = options[:edit] || “edit”  
14 new\_string = options[:new] || “new”  
15 controller\_name = controller.split(’/’).last  
16  
17 mapper.send “#{controller\_name.pluralize}_index",  
 #GET index  
18 plural,  
19 :controller =\> controller,  
20 :action =\> “index”,  
21 :conditions =\> {:method =\> :get}  
22  
23 mapper.send “#{controller\_name.pluralize}”, #POST  
create  
24 plural,  
25 :controller =\> controller,  
26 :action =\> “create”,  
27 :conditions =\> {:method =\> :post}  
28  
29 mapper.send "new_#{controller\_name}”, #GET new  
30 plural+"/#{new\_string}",  
31 :controller =\> controller,  
32 :action =\> “new”,  
33 :conditions =\> {:method =\> :get}  
34  
35 mapper.send “#{controller\_name}”, #GET show  
36 singular +"/:id",  
37 :controller =\> controller,  
38 :action =\> “show”,  
39 :conditions =\> {:method =\> :get}  
40  
41 mapper.send “edit\_#{controller\_name}”, #GET edit  
42 singular+"/:id/#{edit\_string}",  
43 :controller =\> controller,  
44 :action =\> “edit”,  
45 :conditions =\> {:method =\> :get}  
46  
47 mapper.send “#{controller\_name}”, #PUT update  
48 singular+"/:id",  
49 :controller =\> controller,  
50 :action =\> “update”,  
51 :conditions =\> {:method =\> :put}  
52  
53 mapper.send “#{controller\_name}”, #DELETE destroy  
54 singular+"/:id",  
55 :controller =\> controller,  
56 :action =\> “destroy”,  
57 :conditions =\> {:method =\> :delete}  
58 end  
59  
60 custom\_resources map, :controller =\> “admin/room”, :object  
=\> “place”,:new =\> “create\_new”

this will create basic restfull routes like new\_room\_path #=\> /places/  
create\_new  
as you can see, it is ugly, but basicaly working. I dont want ugly  
code in my app. Any sugestions? Some routing method I am missing?

---

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [June 17, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/custom-resources/140606/2 "2008-06-17T14:45:09Z")

</div>

i think you left REST about three miles back down the road. 😉 there’s  
nothing wrong with using what parts of REST you want or ditching the  
whole  
thing and using custom routes to chase the “pretty url” dragon if that’s  
what you wish but REST isn’t going to support all these little “pretty”  
routes very well. YMMV

RSL

---

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [June 18, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/custom-resources/140606/3 "2008-06-18T14:33:18Z")

</div>

well the actual problem i was solwing with all that was localization -  
I name code in english but urls have to be in another (czech)  
language. So i need routing system to generate from path like  
“/uživatelé/1” with GET something like  
:controller =\> “user”, :action =\> “show”, :id =\> 1  
and from  
user\_path(1) url #=\> “uživatelé/1”.

At first the whole routing was based on named routes, but it quickly  
got out of hand with 5+ controllers.
