This project is archived and is in readonly mode.

#5510 ✓wontfix

Polymorphic routes, :as => "polymorphic_name"

Reported by ifesdjeen | August 31st, 2010 @ 10:13 AM

Polymorphic routes problem:

Given I have 2 models:
1) User (has_many messages)
2) Message (belongs_to user), which is polymorphic. It may be received_messages and sent_messages through the intermediate table.

And 3 controllers:
1) UserController
2) ReceivedMessagesController
3) SentMessagesController

If I'm trying to use usual polymorphic _path helpers, user_messages url is received, instead of user_sent_messages. But it doesn't even exist. I use polymorphic controllers along with polymorphic models.

Suggestion is to add :as to polymorphic url helpers, and allow not only edit and new but also index to be accessible. Attached it a gist with diff. Not tested yet. If anyone has an idea how same thing could be implemented in a better way it would be very much appreciated

Comments and changes to this ticket

  • Andrew White

    Andrew White August 31st, 2010 @ 01:31 PM

    • State changed from “new” to “wontfix”
    • Importance changed from “” to “Low”

    The polymorphic_path helper is designed to build route names from the class name of the object passed to it with. Therefore if you pass it a message object then that's what it will use - it doesn't look at the application's routes, it just calls the route helper name that it builds from the arguments. You may want to look at refactoring your routes to something like this:

      # config/routes.rb
      resources :users do
        resources :messages, :except => :index do
          collection do
            get  :received
            get  :sent
            post :send
          member do
            post :send

    Then in MessagesController you can implement the custom actions for received messages, sent messages and sending a message directly or after saving a draft. This way you can map the standard REST actions like new, create, edit, update to dealing with drafts. The destroy action would then either delete an unsent draft message or delete the link record from the appropriate table depending on whether it's a sent or received message.

  • ifesdjeen

    ifesdjeen August 31st, 2010 @ 01:46 PM

    Thanks, Andrew, will try to do so

  • ifesdjeen

    ifesdjeen August 31st, 2010 @ 02:05 PM

    BTW, Andrew, it does look at the application routes, moreover it matches all arguments you pass.
    Actually, what I was trying to implement, is also on top of Inherited Resources.

    Inherited Resources is picking up child objects with polymorphic connections.
    The other problem I ran into in Rails itself, is that polymorphic_path([user, "sent_messages"]) generates an error, that's saying that :destroy action is missing for SentMessages controller, even though it's present in routes. It was caused by further check in Rails, that checks if the model is not persisted, it pops it out of the hash. And that produces an unmatched route.

    What you say is completely valid, but letting 2 controllers to be polymorphic in a way, that's quite close to models polymorphic behavior, I may get a bit more out of it all.

    Thank you for participation again

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>