Andy Gregorowicz
2008-Jun-19 21:07 UTC
How should I fix a problem with uncountable resources in polymorphic_url?
I''ve run across a bug with polymorphic_url, which I have reported here: http://rails.lighthouseapp.com/projects/8994/tickets/411-polymorphic_path-breaks-on-uncountable-resources The gist of the problem is that passing an uncountable resource (such as an AR Model called Sheep) causes the method to throw an exception. The reason for this is because when you map an uncountable resource, you pass in a :singular option. Something like: map.resources :sheep, :singular => :sheep_instance That generates helpers called sheep_instance_url, edit_sheep_instance_url, etc. This becomes an issue for polymorphic_url as it tries to call the named route helper, but has no good way of looking up the appropriate helper. polymorphic_url consults RecordIdentifier, which will try to get the singular name of the model. When this is done for an uncountable model the singular and plural name are the same and build_named_route_call will generate the wrong name for the helper (in the example, if you try polymorphic_url(@sheep) it will try to call sheep_url, when it should be calling sheep_instance_url). I don''t see any good way on checking what the named_routes for an uncountable resource are... Does anyone have any suggestions for fixing this bug? Thanks, Andy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Zachary Carter
2008-Jul-30 06:40 UTC
Re: How should I fix a problem with uncountable resources in polymorphic_url?
I''ve run into this problem also. I''d assume this method[1] would have to be patched so it knows about the :singular option on a resource. I''m new to the Rails internals, so I''m not sure exactly how to accomplish that. I''ll keep digging though. [1] http://rails-doc.org/rails/ActionController/RecordIdentifier/singular_class_name Best, Zach Carter http://zachcarter.info On Thu, Jun 19, 2008 at 5:07 PM, Andy Gregorowicz <gregorowicz@gmail.com> wrote:> > I''ve run across a bug with polymorphic_url, which I have reported > here: > http://rails.lighthouseapp.com/projects/8994/tickets/411-polymorphic_path-breaks-on-uncountable-resources > > The gist of the problem is that passing an uncountable resource (such > as an AR Model called Sheep) causes the method to throw an exception. > The reason for this is because when you map an uncountable resource, > you pass in a :singular option. Something like: > > map.resources :sheep, :singular => :sheep_instance > > That generates helpers called sheep_instance_url, > edit_sheep_instance_url, etc. > > This becomes an issue for polymorphic_url as it tries to call the > named route helper, but has no good way of looking up the appropriate > helper. polymorphic_url consults RecordIdentifier, which will try to > get the singular name of the model. When this is done for an > uncountable model the singular and plural name are the same and > build_named_route_call will generate the wrong name for the helper (in > the example, if you try polymorphic_url(@sheep) it will try to call > sheep_url, when it should be calling sheep_instance_url). I don''t see > any good way on checking what the named_routes for an uncountable > resource are... > > Does anyone have any suggestions for fixing this bug? > > Thanks, > Andy > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew White
2008-Jul-30 10:08 UTC
Re: How should I fix a problem with uncountable resources in polymorphic_url?
On 30 Jul 2008, at 07:40, Zachary Carter wrote:> I''ve run into this problem also. I''d assume this method[1] would have > to be patched so it knows about the :singular option on a resource. > I''m new to the Rails internals, so I''m not sure exactly how to > accomplish that. I''ll keep digging though.A similar problem also applies to using polymorphic_url with singular resource: http://rails.lighthouseapp.com/projects/8994/tickets/267-form_for-for-singular-resource-will-products-plural-invokes#ticket-267-3 As I said in my comment, I can''t see how polymorphic_url could know whether an AR instance is mapped to a singular resource path. For the case of an uncountable resource polymorphic_url should be able to detect whether the AR model is uncountable and call the appropriate url helper. Andrew --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Zachary Carter
2008-Jul-30 18:42 UTC
Re: How should I fix a problem with uncountable resources in polymorphic_url?
Right, it needs the value you specify for the :singular option on the resource to call the correct url helper. For now I''ve patched it to append _instance if singular and plural are the same, because that''s the idiom I use. Ideally it would be able to get the value from somewhere. Best, Zach Carter http://zachcarter.info On Wed, Jul 30, 2008 at 6:08 AM, Andrew White <andyw@pixeltrix.co.uk> wrote:> > > On 30 Jul 2008, at 07:40, Zachary Carter wrote: > >> I''ve run into this problem also. I''d assume this method[1] would have >> to be patched so it knows about the :singular option on a resource. >> I''m new to the Rails internals, so I''m not sure exactly how to >> accomplish that. I''ll keep digging though. > > > A similar problem also applies to using polymorphic_url with singular > resource: > > http://rails.lighthouseapp.com/projects/8994/tickets/267-form_for-for-singular-resource-will-products-plural-invokes#ticket-267-3 > > As I said in my comment, I can''t see how polymorphic_url could know > whether an AR instance is mapped to a singular resource path. For the > case of an uncountable resource polymorphic_url should be able to > detect whether the AR model is uncountable and call the appropriate > url helper. > > > Andrew > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---