ruby-on-railsrubyruby-on-rails-6.1import-maps

Rails 6.1 — add `importmap-rails`, gem bundles but methods unavailable


Ruby: 3.1.2, Rails 6.1

Trying to add gem importmap-rails to an existing Rails 6.1 app.

The gem bundles, but neither the rake importmap:install task nor the gem helper method <%= javascript_import_tags %> are available.

In rails console, the class Importmap is undefined.

It seems the gem does not install properly or completely. No useful error messages raised.

How can I debug a gem that bundles but is not available? The situation doesn't make sense to me.

(Related question from earlier attempt)


Solution

  • The issue was that the importmap-rails gem was inside group :rails in the Gemfile, and so the class did not get required.

    This does not work (linked question above has the explanation):

    # gem("rails", "~> 7.0")
    
    group :rails do
      gem("actioncable")
      # gem("actionmailbox")
      gem("actionmailer")
      gem("actionpack")
      # gem("actiontext")
      gem("actionview")
      gem("activejob")
      gem("activemodel")
      gem("activerecord")
      # gem("activestorage")
      gem("activesupport")
      gem("bundler")
      gem("importmap-rails")
      gem("railties")
      gem("sprockets-rails")
      gem("turbo-rails")
    end
    

    Needs to be:

    group :rails do
      ...
      gem("bundler")
      gem("railties")
    end
    
    gem("importmap-rails")
    gem("sprockets-rails")
    gem("turbo-rails")