Skip to content

Polymorphic associations don't work with namespaced classes #3376

@jacob-carlborg-apoex

Description

@jacob-carlborg-apoex

Given a set of models:

class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true, inverse_of: :pictures
end

module Foo
  class Employee < ApplicationRecord
    has_many :pictures, as: :imageable, inverse_of: :imageable
  end  
end

module Bar
  class Product < ApplicationRecord
    has_many :pictures, as: :imageable, inverse_of: :imageable
  end
end

When creating a new Picture, in the form, after selecting the type, RailsAdmin will fail to load the records for the associations. The problem is that RailsAdmin will get the name of the model:

https://github.com/sferik/rails_admin/blob/238f18ee2386f9858670b7995dcb628b8fe6bde9/lib/rails_admin/config/fields/types/polymorphic_association.rb#L66

Which is then used as the id of an HTML tag:

https://github.com/sferik/rails_admin/blob/238f18ee2386f9858670b7995dcb628b8fe6bde9/app/views/rails_admin/main/_form_polymorphic_association.html.haml#L2

https://github.com/sferik/rails_admin/blob/238f18ee2386f9858670b7995dcb628b8fe6bde9/app/views/rails_admin/main/_form_polymorphic_association.html.haml#L12-L15

https://github.com/sferik/rails_admin/blob/238f18ee2386f9858670b7995dcb628b8fe6bde9/app/views/rails_admin/main/_form_polymorphic_association.html.haml#L23-L24

When calling .name on a model declared in a module, the result will contain colons, i.e. Bar::Product. A string containing : is not a valid to fetch an HTML tag using the id attribute, as the JavaScript does here:

https://github.com/sferik/rails_admin/blob/238f18ee2386f9858670b7995dcb628b8fe6bde9/app/assets/javascripts/rails_admin/ra.widgets.js#L205-L206

I'm using:
rails_admin: 2.2.1
rails: 6.1.4
ruby: 3.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions