#411 new
Andy Gregorowicz

polymorphic_path breaks on uncountable resources

Reported by Andy Gregorowicz | June 13th, 2008 @ 01:59 PM

It seems that passing an uncountable resource to polymorphic_path and associated methods causes an exception to be thrown.

This causes an exception for me on Rails 2.1 under Ruby 1.8.6

Migration

create_table :sheep do |t|
  t.string :name

  t.timestamps
end

Model

class Sheep < ActiveRecord::Base
end

Routes

ActionController::Routing::Routes.draw do |map|
  map.resources :sheep, :singular => :sheep_instance

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

$ script/console

Loading development environment (Rails 2.1.0)

>> sheep = Sheep.create(:name => 'Dolly')

=> #

>> app.polymorphic_path(sheep)

NoMethodError: You have a nil object when you didn't expect it!

The error occurred while evaluating nil.to_sym

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route.rb:145:in `extra_keys'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route.rb:145:in `map'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route.rb:145:in `extra_keys'

from generated code (/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route.rb:45):3:in `generate'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route.rb:122:in `generate'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route_set.rb:337:in `generate'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/url_rewriter.rb:131:in `rewrite_path'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/url_rewriter.rb:110:in `rewrite_url'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/url_rewriter.rb:88:in `rewrite'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/integration.rb:218:in `url_for'

from (eval):17:in `sheep_path'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/polymorphic_routes.rb:101:in `send!'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/polymorphic_routes.rb:101:in `polymorphic_url'

from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/polymorphic_routes.rb:108:in `polymorphic_path'

from (irb):2

Comments and changes to this ticket

  • Andy Gregorowicz

    Andy Gregorowicz June 19th, 2008 @ 10:27 AM

    The problem seems to be that RecordIdentifier is used to determine the singular class name, which is then used to generate the named route call. So for instance, when calling

    
    polymorphic_path(sheep) 
    
    

    RecordIdentifier will be called and come up with the singular class name of sheep and the following route will be called.

    
    sheep_path(sheep) 
    
    

    When it should be

    
    sheep_instance_path(sheep)
    
    

    RecordIdentifier doesn't know about the :singular option passed into the route definition. I'm not sure what would be the best way to fix this... Any suggestions?

  • Zach Carter

    Zach Carter July 30th, 2008 @ 03:39 PM

      • → Tag changed from “” to “2.1 actionpack bug”

    Currently I patch RecordIdentifier to add _instance to the class name if the singular and plural are the same:

    module ActionController  
      module RecordIdentifier
        # Returns the singular class name of a record or class. Examples:
        #
        #   singular_class_name(post)             # => "post"
        #   singular_class_name(Highrise::Person) # => "highrise_person"
        #   singular_class_name(sheep)             # => "sheep_instance"
        def singular_class_name(record_or_class)
          model_name = model_name_from_record_or_class(record_or_class)
          model_name.singular + (model_name.singular == model_name.plural ? '_instance':'')
        end
      end
    end
    

    Still looking for a proper fix.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

People watching this ticket