railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.88k stars 2.25k forks source link

Rails admin edit and create UI not loading correctly without eager_load set to true #3558

Closed rpalackas closed 1 year ago

rpalackas commented 1 year ago

Describe the bug I have a rails 6 application using mongodb. Currently if you edit any of our records in development mode none of editing infrastructure loads correctly. Boolean fields show as editable text, relationships just show id's, etc. This problem wasn't happening on our deployed servers and on further investigation setting eager_load to true everything loads correctly. We would like to be able to use rails admin on local development servers where we leave eager_load false. Is there a way to resolve this without setting eager load to true?

Reproduction steps Navigate rails admin create or edit for an object that relates to other objects in the database. Observe the fields. As an example User has the boolean field Test only:

Screen Shot 2022-10-18 at 2 08 21 PM

User has many authorized sites:

Screen Shot 2022-10-18 at 2 10 55 PM

User belongs to a primary authorized site:

Screen Shot 2022-10-18 at 2 11 20 PM

Expected behavior In comparison if you set the configuration field eager_load to true the fields load correctly for editing:

Screen Shot 2022-10-18 at 2 13 02 PM Screen Shot 2022-10-18 at 2 13 26 PM Screen Shot 2022-10-18 at 2 13 50 PM

we would like this functionality to work correctly without needing to set eager_load to true for development work. Is this a known issue or is something else going wrong?

Additional context

rpalackas commented 1 year ago

Accidentally submitted https://github.com/railsadminteam/rails_admin/issues/3557 before I finished writting it up so I closed it. Sorry for the duplicate

mshibuya commented 1 year ago

How do you configure RailsAdmin? Could you show me what you have in config/initializers/rails_admin.rb and models ?

rpalackas commented 1 year ago

Rails admin config:

RailsAdmin.config do |config|
  config.asset_source = :sprockets
  config.main_app_name = ['Company', 'Admin']
  config.current_user_method { current_user }
  config.authorize_with :cancancan
  config.parent_controller = 'RailsAdminController'
  config.actions do
    dashboard { statistics false }
    send_grid_templates
    generic_enrollment_file_upload
    remove_proconnection_file_upload
    on_board
    admin_tools
    index # mandatory
    new do
      except do
        [VariousModels]
      end
    end
    export
    history_index
    bulk_delete
    # member actions
    show
    edit do
      except do
        [OtherModels]
      end
    end
    delete do
      except do
        [MoreModels]
      end
    end
    history_show
    show_in_app
  end
end

User model relevant rails admin bit:

class user
  include RailsAdmin::User

  belongs_to :primary_authorized_site, class_name: ProviderGroupSite.name,
                                       inverse_of: :primary_authorized_sites
  field :test_only, type: ::Mongoid::Boolean, default: false
  has_and_belongs_to_many :authorized_sites,
                          class_name: ProviderGroupSite.to_s,
                          inverse_of: :users,
                          autosave: true,
                          validate: false
end

User rails admin concern edit portion:

module RailsAdmin::User
  extend ActiveSupport::Concern

  included do
  rails_admin do
    scope { abstract_model.scoped.unscoped }

    list do
      # various list fields here
    end

    show do
      # various show configurations here
    end

    edit do
      group :personal_info do
        # various fields here
        field :test_only do
          label 'Test Only'

           visible do
              User.current_user.admin?
            end
          end
        end
        group :user_info do
           # various fields here
          field :primary_authorized_site
          field :authorized_sites
        end
      end
    end
  end
end
mshibuya commented 1 year ago

Thanks for sharing. I think this will be the same issue with #3490. Can you see if upgrading rails_admin to 3.1.0.rc2 helps?

rpalackas commented 1 year ago

@mshibuya Yep upgrading to 3.1.0.rc2 does resolve that issue. However on that tag my navigation is real screwy not sure if this is a known issue:

Screen Shot 2022-10-21 at 1 37 50 PM

I won't be able to upgrade to a release candidate but I might be able to convince the team to enable eager_load temporarily and we'll upgrade to 3.1 as soon as it fully releases and disable that setting. Do you have a timeline on the 3.1 release?

mshibuya commented 1 year ago

Make sure that you use the latest RailsAdmin JS and CSS. Did you update the rails_admin npm package?

rpalackas commented 1 year ago

@mshibuya no just updated the gem in my Gemfile. Not going to have a change to update the npm package. Thanks for your help this issue can be closed since it is resolved in 3.1

rpalackas commented 1 year ago

For anyone else who runs into this issue there are 2 ways it can be solved without upgrading to rails admin 3.1. The first is as I noted above to enable eager_load in your application configuration. The second is to move your include that adds rails admin configurations to after all of your relationship declarations. This causes the relationships to be loaded first then the rails admin config loads and correctly identifies the field types. So in my above example the user model would change to

class user
  belongs_to :primary_authorized_site, class_name: ProviderGroupSite.name,
                                       inverse_of: :primary_authorized_sites
  field :test_only, type: ::Mongoid::Boolean, default: false
  has_and_belongs_to_many :authorized_sites,
                          class_name: ProviderGroupSite.to_s,
                          inverse_of: :users,
                          autosave: true,
                          validate: false

  include RailsAdmin::User
end

This is the solution we ended up going with which makes our files less nicely organized but doesn't require changes to application configuration.