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

Ticket #7100 (closed defect: duplicate)

Opened 2 years ago

Last modified 11 months ago

map.resources does not take custom inflections into account when creating routes in test mode

Reported by: piotrb Assigned to: core
Priority: normal Milestone: 1.2.5
Component: ActionPack Version: edge
Severity: major Keywords: inflections routes
Cc:

Description

there is a diffrence somewhere between test and dev mode .. since custom inflected named routes generated by map.resources seem to work fine when running the actual app .. and when running controller tests

steps to reproduce:

  • create a fresh app
  • generate a controller (lets call it home)
  • in environment add a custom inflection .. my problem case was "tax" => "taxes"
    Inflector.inflections do |inflect|
      inflect.irregular 'tax', 'taxes'
    end
    
  • create a new resource route for our non-existant tax model (doesn't seem to matter that it exists, so I'm ommiting creating the model for simplicity)
    map.resources :taxes
    
  • in the controller, create an action (lets call if foo), and lets try to generate a url to tax id: 1
      def foo
        render :text => tax_path(1)
      end
    
  • fill in a really basic test for the controller
      def test_foo
        get :foo
      end
    
  • adjust the test database to something that exists (or sqlite) just so it keeps some default test code happy that tries to use the db

ok .. so this is all the prep .. now boot up the app, browse to /home/foo, you should be greeted with "/taxes/1" .. in other words it worked

now try to run the test ..

NoMethodError: undefined method `tax_path' for #<HomeController:0x221075c>
    {rails_root}/config/../app/controllers/home_controller.rb:4:in `foo'

now some after checks

in ./script/console .. running "taxes".singularize yields "taxis" (without the custom inflection) .. (thats why I have the custom inflection) .. anyways .. going into the controller .. and changing "tax_path" to "taxis_path" .. makes it work in testing just fine (but it now crashes in dev)

Change History

01/17/07 02:06:13 changed by hasmanyjosh

If this is working for you in dev mode, you probably have something weird going on in environments/development.rb. The problem is that custom inflections don't get initialized until after routes have been initialized. I've run into this myself (I'm surprised there isn't a ticket on it already, guess I got lazy). As a workaround you can define your custom inflections in the particular environment files (development.rb, test.rb, etc), as they are loaded before route generation.

01/17/07 03:15:33 changed by piotrb

my repro steps are based on a clean edge app .. with no touching of the environments ..

maybe mongrel_rails does something weird with the env that causes it to load env first then ..

my workaround was more direct .. I dumped my inflections right on top of my routes .. ugly but worked

01/20/07 18:37:37 changed by dcmanges

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

This has to do with your routes being generated before you set the custom inflection. As you mentioned, you can fix this by setting the custom inflection before the routes are generated - see duplicate report and patch in #6829

10/08/07 01:23:23 changed by bitsweat

  • keywords set to inflections routes.
  • milestone changed from 1.2 to 1.2.5.