audits1984 icon indicating copy to clipboard operation
audits1984 copied to clipboard

undefined method `assets' for #<Rails::Application::Configuration

Open AlicjaCyganiewicz opened this issue 2 years ago • 18 comments

As we're not using # require "sprockets/railtie" - I've encountered a problem with audits1984 engine: undefined method 'assets' for #<Rails::Application::Configuration

I fixed it locally by conditional statement

initializer "audits1984.assets.precompile" do |app|
  if app.config.respond_to?(:assets)
    app.config.assets.precompile << "audits1984_manifest.js"
  end
end

Would that be a problem to merge it into code base 🥺 ? Cheers!

AlicjaCyganiewicz avatar Sep 08 '23 13:09 AlicjaCyganiewicz

Hey @AlicjaCyganiewicz I'm hitting the same error. May I ask where you did add this conditional? Did you use a fork of the gem?

nicalpi avatar Oct 24 '23 15:10 nicalpi

@nicalpi I had the same issue and had to fork the gem with that conditional. And then added the following initializer to fix the styling of the UI.

# config/initializers/audits1984.rb

unless Rails.application.config.respond_to?(:assets)
  gem_asset_root = Audits1984::Engine.root.join("app/assets")
  tmp_asset_root = Rails.root.join("tmp/audits1984/assets")

  # Create audits1984 css and js files
  asset_files = [
    "javascripts/audits1984/application.js",
    "stylesheets/audits1984/bulma.min.css",
    "stylesheets/audits1984/application.css",
  ]

  asset_files.each do |file|
    unless (local_file = tmp_asset_root.join(file)).exist?
      local_file.dirname.mkpath
      local_file.write gem_asset_root.join(File.dirname(file)).children.map(&:read).join("\n\n")
    end
  end

  # Serve custom assets instead of audits1984's (that will 404 without the asset pipeline)
  Rails.application.config.middleware.use Rack::Static,
    urls: %w[/javascripts/audits1984 /stylesheets/audits1984 /images/audits1984],
    root: tmp_asset_root
end

coalest avatar Oct 31 '23 16:10 coalest

Nice, thanks a lot @coalest. I ended up doing something similar in the end, so it's good to know I'm not the only one with the same issue.

nicalpi avatar Oct 31 '23 17:10 nicalpi

We've had to do the same, definitely feels like something that should be fixed without forking

stephwestgarth avatar Aug 26 '24 03:08 stephwestgarth

Sorry, I'm reopening this one as the patch I had merged wasn't really feasible as a generic solution for the gem.

If you're not using propshaft/sprockets, what are you using? I'd be willing to add support for import maps such as we do in Mission Control jobs (see this and this). Is someone willing to work on that?

jorgemanrubia avatar Nov 11 '24 16:11 jorgemanrubia

@jorgemanrubia Our asset pipeline is outsourced to a CDN, packaged along with our Vue frontend. For serving other assets, like the Sidekiq Web UI, we serve it directly through Rack, similar to how the PR above does it.

I'm also open to having an API-only mode, where there is no UI.

humzahkiani-JW avatar Nov 11 '24 18:11 humzahkiani-JW

On our end, we're using vite_rails in that app.

Isn't it risky to end up having tons of additional implementations in the audits1984 repo, for a bunch of different ways to serve assets? Maybe just exposing some hooks or something would be the way to go?

jipiboily avatar Nov 11 '24 19:11 jipiboily

@rosa just addressed this same problem for Mission control jobs. I'd like to use the same approach here. I'll try to get to this but I have other things in my queue, so I'd appreciate a PR if someone feels like working on this.

jorgemanrubia avatar Nov 12 '24 09:11 jorgemanrubia

I'll have a look, @jorgemanrubia! 🙏 Also, @jipiboily, I realised I forgot about vite_rails in that other PR. I need to look into how that one works, as I'm not familiar with it, and see how the solution could be expanded to support it.

rosa avatar Nov 12 '24 09:11 rosa

@jipiboily, I spent some time looking into vite_rails and testing this and it seems you can use propshaft together with that without any issues, so I think I'm going to pursue the same approach here as for Mission Control, like @jorgemanrubia suggested!

rosa avatar Nov 12 '24 13:11 rosa

@AlicjaCyganiewicz @nicalpi @coalest @humzahkiani-JW @jipiboily, if you want to give a try at #61 please do! I hope it fixes your issues. Basically, just add propshaft to your app with that branch.

rosa avatar Nov 13 '24 15:11 rosa

@rosa it appears to work with propshaft for the most part! The only issue I am seeing is the CSS styling missing in the UI.

Screenshot 2024-11-13 at 4 21 12 PM

humzahkiani-JW avatar Nov 13 '24 21:11 humzahkiani-JW

Ohh, unfortunately that means it's not quite working as it should, as the CSS are one of the things that should be working fine thanks to importmaps and propshaft. Could you check the request to fetch the CSS in your network tab and find the corresponding request in your local server to see why it's not finding it?

rosa avatar Nov 13 '24 21:11 rosa

It appears that the CSS requests are being mistakenly routed to our react frontend, which returns the default react HTML page. I think this is probably caused by how our Nginx is setup. I can try setting this up on a more standard Rails service we have.

Does this match what a typical path should be for the CSS request? /assets/audits1984/bulma.min-a0bcd487.css

humzahkiani-JW avatar Nov 13 '24 23:11 humzahkiani-JW

That did it!

Screenshot 2024-11-13 at 6 05 59 PM

humzahkiani-JW avatar Nov 13 '24 23:11 humzahkiani-JW

Thank you so much @rosa 🙏 🙏🙏🙏. I merged https://github.com/basecamp/audits1984/pull/61 and will release a new version with it later today.

jorgemanrubia avatar Nov 14 '24 09:11 jorgemanrubia

Thank you both for your help here!

humzahkiani-JW avatar Nov 14 '24 17:11 humzahkiani-JW

https://github.com/basecamp/audits1984/issues/50#issuecomment-2475775249

Hi is there any plan to release this soon?

qingemeng avatar Mar 12 '25 12:03 qingemeng