Search code examples
ruby-on-railsrubyroutessingle-table-inheritance

How do I force Rails to use the superclass name in link_to helpers when using STI?


I'm using STI with a Rails 3.2 app. I want to force Rails to use the superclass name in link_to helpers (or any where else when it's generating paths) and not the subclass name.

So, <%= link_to current_user.name, current_user %> produces /:class_name/:id (class name can be "Moderator," "Member," etc...).

I would like it to produce /users/:id, where users does not change to the name of the subclass. I know I can change current_user to user_path(current_user), but I prefer to use the shortcut, letting Rails figure it out.

Is this possible?


Solution

  • I think you should define url helpers, something like this

    def moderator_url record
      user_url record
    end
    

    Or just use aliases

    alias :moderator_url :user_url
    

    This is code which rails use for url generation when you pass a record as a option

    https://github.com/rails/rails/blob/537ede912895d421b24acfcbc86daf08f8f22157/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb#L90