Home > Software engineering >  Cannot find rails_admin.css in Rail 5.4.2
Cannot find rails_admin.css in Rail 5.4.2

Time:09-28

Rails Admin works locally for me, but when I deploy to Heroku, it fails with:

ActionView::Template::Error (The asset "rails_admin/rails_admin.css" is not present in the asset pipeline

I am on Rails 5.2.6 and Rails Admin 2.0.2

I added initializers/assets.rb with this line: Rails.application.config.assets.precompile = ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']

but those files do not exist in assets/javascripts and assets/stylessheets.

This app was originally built as an API only app, but I've added all the missing middleware to make it work as a normal Rails app.

This is my application.rb:


require_relative 'boot'

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module CheckwizApi
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Only loads a smaller set of middleware suitable for API only apps.
    # Middleware like session, flash, cookies can be added back manually.
    # Skip views, helpers and assets when generating a new resource.
    config.api_only = false

    # use uuid's as primary_key
    config.active_record.primary_key = :uuid
    config.generators do |g|
      g.orm :active_record, primary_key_type: :uuid
    end
    
    config.i18n.default_locale = :en    

    config.active_job.queue_adapter = :sidekiq

    # Added because RailsAdmin needs them
    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Flash
    config.middleware.use Rack::MethodOverride
    config.middleware.use ActionDispatch::Session::CookieStore
  end
end

Note that I have set config.api_only = false and added back the required middleware, but to no avail. If I set it to true, same error. If I remove the assets file I added above, same error.

The assets/javascripts and assets/stylesheets folders are empty. Is that the problem?

I have looked at several other closed issues related to this and most of them advise adding the above middleware, or clearing tmp/cache and restarting. Neither worked for me sadly.

CodePudding user response:

A number of things had to be done to fix this.

Firstly, I updated my config/initializers/assets.rb file to this:

Rails.application.config.assets.enabled = true
# Prevent initializing the application before assets are precompiled (required for heroku)
Rails.application.config.assets.initialize_on_precompile = false
# Add Rails Admin assets (required)
Rails.application.config.assets.precompile  = ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']

Then, by examining the Rails logs on Heroku:

heroku logs -t --remote production

I spotted this line:

Detected manifest file, assuming assets were compiled locally

So inside public/assets I deleted the manifest file (it is created if you compile assets locally, which perhaps I had done once to try to solve this issue, and forgot it was there).

This then caused the following error to appear in the heroku log:

Sprockets::ArgumentError: link_directory argument must be a directory

So I then added a hidden empty file called .keep to both the javascripts and stylesheets folders inside app/assets

Now Heroku runs the asset precompile task as part of the deploy (ie. whilst building the slug) and the rails_admin js and css files are created.

More info here and here which helped figure this out.

  • Related