Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10578 (closed enhancement: fixed)

Opened 8 months ago

Last modified 5 months ago

[PATCH] path_substitute option for map.resources to customise URL

Reported by: blj Assigned to: core
Priority: normal Milestone: 2.1
Component: ActionPack Version: edge
Severity: normal Keywords: verified resources url path
Cc: masterkain@gmail.com, shingara

Description

This is a patch that would add an option :path_replace to map.resources.

This would help in creating customised URL's irrespective of what the resources were called.

For e.g. if I want to keep my resources in English, but the URLs to talk spanish, then:

    
  map.resources :products, :path_replace=>'productos' do |product|
    product.resources :product_reviews, :path_replace => 'comentarios'
  end 
  map.resources :tutors, :path_replace=>'tutores' do |tutor|
    product.resources :tutor_reviews, :path_replace => 'comentarios'
  end 

These will yield paths like the following:

  productos/:product_id/comentarios
  tutores/:tutor_id/comentarios

Attachments

patch-resources-path-replace.diff (5.7 kB) - added by blj on 12/20/07 21:20:54.
Patch to actionpack/resources
patch-resources-path-substitute.diff (5.8 kB) - added by blj on 01/10/08 09:45:26.
renamed path_replace to path_substitute
patch-resources-path-segment.diff (5.7 kB) - added by blj on 01/29/08 06:20:56.
renamed path_substitute to path_segment

Change History

12/20/07 21:20:54 changed by blj

  • attachment patch-resources-path-replace.diff added.

Patch to actionpack/resources

12/20/07 21:22:40 changed by blj

  • type changed from defect to enhancement.

12/20/07 21:27:58 changed by RSL

I could see this being useful as well for when clients/whoever suddenly want the URI to use a different resource name [but keep the same routing]. Looks like it would save the pain of renaming the controllers, and helpers, and tests, and ...

+1

12/30/07 07:12:10 changed by blj

  • milestone deleted.

12/30/07 13:44:49 changed by masterkain

I need it badly, great improvement.

+1

12/30/07 13:46:01 changed by masterkain

  • cc set to masterkain@gmail.com.

12/31/07 12:12:30 changed by blj

  • keywords changed from resources url path to unverified resources url path.

01/03/08 14:50:07 changed by masterkain

I'm building (another) real estate web application with rails and I like to conform to standards, eg: use english language for my coding, named routes, database names, tables and so on.

hovewer something like /rents/1/agency doesn't fit for another language such Italian, I would like to keep routes under the hood as is, but have translated URLs, that are not only useful for users, but also for SEO.

01/09/08 14:53:15 changed by jerome

verified

love it too

+1

(follow-up: ↓ 13 ) 01/09/08 15:15:20 changed by jerome

01/09/08 16:02:25 changed by shingara

  • cc changed from masterkain@gmail.com to masterkain@gmail.com, shingara.

01/09/08 17:18:00 changed by blj

  • keywords changed from unverified resources url path to verified resources url path.

01/09/08 17:18:44 changed by blj

  • keywords changed from verified resources url path to unverified resources url path.

(in reply to: ↑ 9 ) 01/09/08 17:22:21 changed by blj

Replying to jerome:

Wrote http://pastie.caboo.se/private/kikhrfegnrowozrs5jdjja but you got the tests :)

jerome, are you suggesting change the name to path_alias?

(follow-up: ↓ 16 ) 01/09/08 17:26:46 changed by blj

I am unsure about the path_alias and not happy with path_replace either, perhaps path_substitute ?

01/09/08 17:32:04 changed by blj

  • keywords changed from unverified resources url path to Verified resources url path.

(in reply to: ↑ 14 ; follow-up: ↓ 17 ) 01/09/08 22:05:36 changed by jerome

Replying to blj:

I am unsure about the path_alias and not happy with path_replace either, perhaps path_substitute ?

Right. Because now I think path_alias would make people believe that the former path is still available. This is indeed a substitution. Then :path_substitute is a good symbol.

Sure the former path is available but it won't work as a resource anymore according to rake routes (or am I wrong?).

What do you think ?

(in reply to: ↑ 16 ; follow-up: ↓ 18 ) 01/10/08 08:48:09 changed by blj

Replying to jerome:

Sure the former path is available but it won't work as a resource anymore according to rake routes (or am I wrong?). What do you think ?

The former path won't be available, it is completely replaced by the substitute. I will change it to substitute, and steal the doc from your pastie. Thanks.

(in reply to: ↑ 17 ; follow-up: ↓ 19 ) 01/10/08 09:15:39 changed by jerome

Replying to blj:

The former path won't be available, it is completely replaced by the substitute.

Not completely since the resource name matches the controller name. So the path is available but without any restful controls rails does regarding the four HTTP methods.

(in reply to: ↑ 18 ) 01/10/08 09:29:45 changed by blj

Replying to jerome:

Not completely since the resource name matches the controller name. So the path is available but without any restful controls rails does regarding the four HTTP methods.

I hope we are talking about the path here (i.e. the one in the URL). for e.g.

map.resources :products, :path_substitute=>'productos' => GET /productos

GET /products => Routing error

And of course the helper functions are still called as products_url not productos_url.

01/10/08 09:39:04 changed by jerome

Yes we're both talking about the URL; did you keep the generic mapping at the end of routes.rb ?

BTW, I haven't test this patch with nested resources; did you ?

(follow-up: ↓ 23 ) 01/10/08 09:44:29 changed by blj

I guessed you had the generic map at the end. It is separate and it is not necessary, infact I never had one.

Yes I have tested the nested routes, singleton as well and using a namespace.

Am adding a patch now with "substitute" instead "replace". Hope you can test that once.

01/10/08 09:45:26 changed by blj

  • attachment patch-resources-path-substitute.diff added.

renamed path_replace to path_substitute

01/10/08 09:46:49 changed by blj

  • summary changed from [PATCH] path_replace option for map.resources to customise URL to [PATCH] path_substitute option for map.resources to customise URL.

(in reply to: ↑ 21 ) 01/10/08 11:55:44 changed by jerome

Replying to blj:

I guessed you had the generic map at the end. It is separate and it is not necessary, infact I never had one.

So that's why the call to the real controller name still works on my app.

Having tested your last patch (+1 again)

01/10/08 13:42:16 changed by blj

  • keywords changed from Verified resources url path to verified resources url path.

01/10/08 15:35:57 changed by blj

jerome, why not just :path instead of :path_substitute, :path_replace, :path_alias or :resource_uri?

lifo#rubyonrails suggested :resource_uri

(follow-up: ↓ 27 ) 01/10/08 16:11:13 changed by jerome

:path is not enough meaningful in this ocean of options :D

(in reply to: ↑ 26 ) 01/10/08 16:13:34 changed by blj

Replying to jerome:

:path is not enough meaningful in this ocean of options :D

:) OK, will leave it as it is.

(follow-ups: ↓ 29 ↓ 30 ) 01/13/08 03:15:23 changed by leandro

  • priority changed from normal to low.
  • type changed from enhancement to defect.

Ok, so I've used both :path_replace as :path_substitute but no one worked over here. I just coded the following in my routes.rb:

map.resources :articles, :path_substitute => 'artigos'

(in reply to: ↑ 28 ) 01/13/08 08:04:34 changed by jerome

  • type changed from defect to enhancement.

Replying to leandro:

Ok, so I've used both :path_replace as :path_substitute but no one worked over here. I just coded the following in my routes.rb: map.resources :articles, :path_substitute => 'artigos'

Did your really apply the second patch (which has :path_substitute and not :path_replace) ?

(BTW why did you change the ticked type?)

(in reply to: ↑ 28 ; follow-up: ↓ 31 ) 01/15/08 10:59:33 changed by blj

  • priority changed from low to normal.

Replying to leandro:

Ok, so I've used both :path_replace as :path_substitute but no one worked over here. I just coded the following in my routes.rb: map.resources :articles, :path_substitute => 'artigos'

Did you apply any patch at all? Please do not change the ticket type and priority if you unsure. Thanks.

(in reply to: ↑ 30 ) 01/15/08 15:02:32 changed by leandro

Replying to blj:

Did you apply any patch at all? Please do not change the ticket type and priority if you unsure. Thanks.

My bad, very sorry. My issue was that I'm using Rails:freeze:edge, so I forgot to apply the patch on rails files located in my project.

It's all good now.

01/25/08 23:28:03 changed by lifofifo

  • keywords changed from verified resources url path to resources url path.

I was having a discussion with nzkoz about the patch and he likes the idea. But this needs more inputs on the name :path_replace. I'm removing "verified" keyword for the timebeing. Could you please make a post to rails core list asking for suggestions ? If there are not enough opinions, just add the keyword "verified" again.

01/26/08 12:09:36 changed by blj

lifofifo:

thanks, here we go to: Rails core discussion about this topic.

01/29/08 06:20:56 changed by blj

  • attachment patch-resources-path-segment.diff added.

renamed path_substitute to path_segment

01/29/08 12:44:56 changed by blj

  • keywords changed from resources url path to verified resources url path.

01/29/08 18:51:10 changed by jerome

blj: in this quest for an almost perfect argument name (this is so lame :) ), I agree with :path_segment which is true in a semantic point of view

01/29/08 18:52:06 changed by jerome

and I'm glad this is not affected by this recent patch about speeding routes...

01/30/08 04:20:58 changed by blj

jerome: it makes me laugh when you call it a lame quest. Now that we have a patch for every name, I will leave it to the gods to apply which ever suits their taste. Yes the speedy patch does not affect the resources. Mostly that was to do with routing.

02/02/08 05:57:13 changed by bitsweat

  • milestone set to 2.1.

02/02/08 05:57:19 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [8785]) Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'. Closes #10578.

03/31/08 09:23:05 changed by jerome