Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Router in Rails 3 pluralizes collection named routes twice

#4704

I have inflections for the Spanish language.
A model named 'Cliente' pluralizes to "clientes" and this should be the name of the controller.
Everything works well with the inflections, except with the new router.

With the new router

NewApp::Application.routes.draw 
  resources :clentes
end
This fails, it generates routes for a controller named "clienteses" and that it is not correct.

And I have to use the old router, using

NewApp::Application.routes.draw do |map|
  map.resources :clentes
end

An this works, but I can use the improved features of the router.

Reported by Juanma Cervera · May 26th, 2010 @ 11:42 AM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to invalid

    You have to fix this in your application doing your own inflections. Thanks.

    May 26th, 2010 @ 05:04 PM

  2. Juanma Cervera
    Juanma Cervera

    Hi Santiago.
    I don't know what you mean.
    I already have an initializer with the inflections for the language of my app.

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.plural( /$/, 'es')
      inflect.plural(/z$/i, 'ces')
      inflect.plural(/([aeiou])$/i, '\1s')
    
      inflect.singular(/s$/i, '')
      inflect.singular(/es$/i, '')
      inflect.singular(/ces$/i, 'z')
      inflect.singular(/([tj])es$/i, '\1e')
    
    end
    

    And it works great, for all the application, except in the routes, and only when I use the new router syntax.
    Also works well when I use the old router syntax, and this make me think of a problem.

    Juanma Cervera

    May 27th, 2010 @ 07:48 AM

  3. Andrew White
    Andrew White

    If you compare the deprecated mapper to the new mapper then you can see there's an extra call to pluralize which is causing the problem.

    My Spanish isn't up to the task but if you adjust the first inflect.plural to not match words ending 'es' then that should fix your problem as invoking pluralize on English plural words doesn't double pluralize them.

    June 7th, 2010 @ 11:42 AM

  4. gucki
    gucki

    I'm facing the same error, using a fresh rails project using latest beta4 and no custom inflections. Please see here:
    #4857 Router in Rails 3 doesn't use inflections properly *really*

    June 14th, 2010 @ 12:24 PM

  5. javierm
    javierm

    My Spanish isn't up to the task but if you adjust the first inflect.plural to not match words ending 'es' then that should fix your problem

    Hello, Andrew.

    I'm spanish as well and am using similar inflection rules, so I've got the same problem.

    Unfortunately, your suggestion isn't free of troubles. In spanish, there are words that are singular and end in "s", like "res", whose plural is "reses" (same rule as any other word ending in a consonant).

    I know I can add irregular plurals, but then I guess that if we use them for regular plurals we miss the point of defining expressions about how to make plurals.

    This bug is mark as "invalid", but I don't think it should be. "wontfix" maybe, but Rails is supposed to have rules such as "singular form for member path and plural form for collection path", and here it is creating "singular form for member path and plural of the plural for collection path", because of the extra call to pluralize that you mention.

    Thanks!

    August 2nd, 2010 @ 05:48 PM

  6. javierm
    javierm

    Apparently it also happens with english words, even if I don't use any inflections.

    The plural of "taxi" is "taxis", but the plural of "taxis" is "taxes". So if I generate a "taxi" scaffold, "taxis_path" is not generated, but "taxes_path" is.

    Unfortunately I haven't been able to come with a valid test case. Test cases for taxes are already there, and adding test cases for taxis ends up in failure, but I think it fails because of a conflict with the already defined routes for taxes.

    I hope you take this as "trying to help" instead of "spamming on that thing nobody cares about", cause that's my intention ;). If you don't want to fix it, hey, it's your app, you have your priorities.

    August 2nd, 2010 @ 09:03 PM

  7. javierm
    javierm
    • Title changed from Router in Rails 3 doesn't use inflections properly to Router in Rails 3 pluralizes collection named routes twice

    I think I finally figured out where to write the test I wanted. I attach a failing test, patched against v3.0.0_RC (sorry, I couldn't get master to work; bundle failed).

    The first assertion works, but the other two fail.

    Thanks.

    August 4th, 2010 @ 09:00 PM

  8. Andrew White
    Andrew White
    • State changed from invalid to open
    • Assigned user set to Andrew White
    • Importance changed from to Low

    The problem with your test is that taxis (as in the minicab) doesn't have it's own rule - once you add the custom irregular inflection everything works out and it's unlikely you're going to be writing an app with deals with minicabs and surgery together. What we need is a test where two words are likely to be used together and their pluralization overlaps similar to taxi(s) and tax(is|es) - it doesn't matter if they're Spanish.

    Anyway, it's really a good idea to get your inflections setup so that they are idempotent as you never know when you'll end up pluralizing an already pluralized word in your own code.

    August 5th, 2010 @ 07:45 AM

  9. javierm
    javierm

    Hi, Andrew.

    Didn't see your answer before, but in the meantime I've created a patch that makes the test pass and doesn't break any other tests (at least in action pack; couldn't test the whole suite):

    http://gist.github.com/510293

    August 5th, 2010 @ 09:06 PM

  10. javierm
    javierm

    What we need is a test where two words are likely to be used together and their pluralization overlaps similar to taxi(s) and tax(is|es) - it doesn't matter if they're Spanish.

    I'm afraid I don't understand what you mean. Since I'm not sure we're talking about the same thing, I'll try to explain my problem.

    The problem happens with almost any spanish word. The plural of "cliente" is "clientes". And grammar rules says that, if it existed, the plural of "clientes" would be "clienteses" (and the plural of "clienteses" would be "clienteseses", and so on), since words ending with a consonant make (usually) the plural with "-es",

    And that's the original problem: applying spanish rules to the inflections made "clienteses_path" to be generated, instead of "clientes_path".

    Hope it helps. Tell me if I'm understanding you wrong.

    Thanks.

    August 5th, 2010 @ 09:18 PM

  11. javierm
  12. Andrew White
    Andrew White
    • Milestone cleared.
    • Tag set to rails 3.0, patch, resources, routing

    Javier, I've taken your patch and expanded it a bit - don't worry you're still marked as the author.

    Anyway the attached patch tests both the medical term and transport term and refactors the resource name methods a bit. The patch applies cleanly to master and stable.

    August 18th, 2010 @ 01:45 PM

  13. javierm
    javierm

    Awesome! Thank you, Andrew.

    It's ok if you figure as the author anyway. I wouldn't have come up with such a complete patch, after all.

    August 18th, 2010 @ 04:03 PM

  14. Repository
  15. Repository
  16. Jeremy Kemper