Skip to content


Subversion checkout URL

You can clone with
Download ZIP


"polymorphic_path" and "polymorphic_url" support for namespaced classes #5857

Backoo opened this Issue · 8 comments

3 participants


My message is more a pull request than a issue: it would be very useful to add polymorphic_path and polymorphic_url support for namespaced classes.


I'd say more a feature request than a pull request - there's no code attached. :trollface:

Anyway, can you elaborate further what you mean by support for namespaced classes? Some examples of how it would be used for example.


I mean, for example, polymorphic_path and polymorphic_url should work as expected both for Article and Articles::Category as well as that should be. At this time those work only for not namespaced classes like Article.

BTW: For the next time, where I can submit a feature request?


A quick test shows that polymorphic_path(Articles::Category) calls the articles_categories_path so please give an example of it not working how you would expect it to - include routes and models.

Feature requests are okay but you need to put your case if you're asking someone else to do the coding, so outline all the cases where something doesn't work as well as it should do and describe how the feature would be used. For the latter it's best to imagine that you're writing the documentation/readme for that feature.

A more complicated example:

Given a namespaced class like Articles::Categories::Relationship for which

class Articles::Categories::Relationship < ActiveRecord::Base


$ rake routes
article_category_relationships GET          /articles/:article_id/category_relationships(.:format)          articles/categories/category_relationships#index

When I use polymorphic_path(Articles::Categories::Relationship)
Then I get the following error (note: the "articles" word in the generated path below is different than "article" outputed in the rake task above):

undefined method `articles_category_relationships_path' for #<#<Class:0x000...>

Furthermore, at this time it seems not possible to pass the :article_id parameter to the polymorphic method.

And for another context when using an object instance...

To take advantage of the dynamic polymorphic method and to make it to work as expected, for example, for an Article instance method I must use the following "uncommon" statement:

polymorphic_path([@article, 'category_relationships'])

In your last example, what's the behavior you're expecting? I can't imagine a scenario where polymorphic_path(Articles::Categories::Relationship) could return a sensible value - what would :article_id be set to?


@speaking what's wrong with using the article_category_relationships_path(@article) url helper? The polymorphic url helpers are there for things like form_for @model or link_to '…', @model - they're not meant to be used instead of the standard url helpers. You should not be using polymorphic_path to construct a link to an index page.


YOU: what would :article_id be set to?
:article_id would be set to an Integer value. That is, the id of an Article class instance.

YOU: "what's wrong with [...]?"
It is wrong that I cannot use the polymorphic_path to generate a working path/URL like article_category_relationships_path(@article) for reasons explained in my previous post.

YOU: "they're not meant to be used instead of the standard url helpers"
I disagree, because polymorphic_path could be helpful when you have to implement "universal view" files to keep things DRY.


@speaking I think you're asking too much of polymorphic_path - it's not a generic url builder, just a shorthand when you're using RESTful conventions. If you really want a generic url helper for some generic view then just build your own helper - it'll be much faster, e.g:

def record_url(record, options = {})
  case record
  when Articles::Category::Relationship
  # other classes
    polymorphic_url(record, options)

What you're after would tie models and urls too much, e.g. polymorphic_url would need to about a model's foreign keys. This isn't going to happen so I'm closing the issue. Please feel free to open a new issue if you find a specific issue with polymorphic_url and have a concrete example using a straightforward RESTful url.

@pixeltrix pixeltrix closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.