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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 On Mon, Jun 16, 2008 at 1:03 PM, DarkTatka <DarkTatka-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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? > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.
On Jun 17, 2:44 pm, "Russell Norris"
<r...-ftMzyaTR+bHNyFkoKTPOtdi2O/JbrIOy@public.gmane.org>
wrote:> 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
>
> On Mon, Jun 16, 2008 at 1:03 PM, DarkTatka
<DarkTa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > 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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---