spohlenz / tinymce-rails

Integration of TinyMCE with the Rails asset pipeline
Other
814 stars 257 forks source link

Sprockets::Rails::Environment::NoDigestError when using Rails 4.2.0.beta4 in development mode #162

Closed frenkel closed 2 years ago

frenkel commented 9 years ago

I receive an Sprockets::Rails::Environment::NoDigestError exception when trying to use this gem in a new Rails 4.2.0.beta4 project. The following two files can not be loaded because of this exception:

http://localhost:3000/assets/tinymce/themes/modern/theme.js
http://localhost:3000/assets/tinymce/plugins/link/plugin.js

The exact error is:

Sprockets::Rails::Environment::NoDigestError
Assets should not be requested directly without their digests: Use the helpers in ActionView::Helpers to request tinymce/themes/modern/theme.js
Extracted source (around line #22):
  path = path.sub("-#{fingerprint}", '')
else
  raise NoDigestError.new(path)
end
asset = find_asset(path)

How can I fix this?

frenkel commented 9 years ago

Apparently config.assets.digest is now true in development mode as well. Due to the fact that the asset helpers are not loaded, this will result in the error. I've worked around it for now by setting config.assets.digest = false in environments/development.rb

spohlenz commented 9 years ago

Looks related to this: https://github.com/rails/sprockets-rails/issues/103

spohlenz commented 9 years ago

and this pull request: https://github.com/rails/sprockets-rails/pull/149

spohlenz commented 9 years ago

I've implemented a fix in 97b0cec1900787852558832be7803130d6fb62d0. Would you be able to give the master branch a go and let me know if you encounter any issues.

A new release 4.1.6 is well overdue so I will make a new release once I have confirmation that this fixes things.

frenkel commented 9 years ago

I can confirm that your fix works, as the error has gone away using master. Thanks for the quick response, right on time before the official 4.2.0 release :)

frenkel commented 9 years ago

It seems the fix broke TinyMCE combined with Turbolinks, also in production mode. If I set config.assets.digest = false again, TinyMCE works without problems locally.

spohlenz commented 9 years ago

This seems to be working okay for me. Are you able to upload a test app that demonstrates the issue?

frenkel commented 9 years ago

You can see the problem here: https://github.com/frenkel/tinymce-rails-problem-demo Most of the time, the JavaScript error is TypeError: Theme is not a constructor

spohlenz commented 9 years ago

Thanks @frenkel. I'll dive deeper into this tomorrow but I can at least now reproduce the issue.

frenkel commented 9 years ago

Great, thanks for your effort!

spohlenz commented 9 years ago

It looks like this isn't actually related to the original issue and fix, and is more of a general turbolinks-related issue. For me, the issue occurred regardless of config.assets.digest.

The fix which worked for me is to move the JavaScript includes (both application.js and tinymce_assets) into the document HEAD. I'm not deeply familiar with turbolinks but this doesn't appear to be avoidable.

I also had to add the following code to application.js to properly remove the editor instance, allowing it to reload when the page was revisited.

$(document).on('page:receive', function() {
  tinymce.remove();
});
frenkel commented 9 years ago

Thanks for looking into this. However, moving things to the head is not possible for a lot of other scripts in my application. Also, the page blocks when loading if you put your javascript in the head. Is there no way to make this work in the footer?

Thanks!

spohlenz commented 9 years ago

It's not ideal, I agree, but it appears to be a turbolinks limitation.

As a rule of thumb when switching to Turbolinks, move all of your javascript tags inside the head and then work backwards, only moving javascript code back to the body if absolutely necessary.

https://github.com/rails/turbolinks#evaluating-script-tags

frenkel commented 9 years ago

Ok, well, thanks for looking into this.