简体   繁体   中英

Use Rails asset helper to run a Javascript plug in

dealing with an interesting problem. I'm using a third party plug in, Galleria. It runs from the body of the view like so

<script>
Galleria.loadTheme('/assets/galleria/themes/classic/galleria.classic.min.js');
Galleria.run('.galleria'...
</script>

I've realized that with the Rails asset pipeline, I have to use helper tags, otherwise the asset doesn't load. Therefore I subbed the first line out like so:

Galleria.loadTheme("<%= javascript_include_tag 'galleria.classic.min.js', '/galleria/themes/classic' %>");

But I'm not sure what to do about the Galleria.run('.galleria' part, since that's not an asset, that's telling it to run the thing that it should have loaded. Leaving it as is has not worked, so wanted to see if there were any other ideas.

EDIT: What ended up working was this lovely post from someone else, + a few bits that I had to add on myself: How to use Galleria plugin with Rails 4 Pipeline

I think you want

Galleria.loadTheme("<%= javascript_include_tag '/galleria/themes/classicgalleria.classic.min.js'%>")

Assuming you've put the js in assets/javascripts?

You may need to add something to the assets path for compilation. I think you can't do the javascript_include_tag directly in line. That should generate a full <script></script> tag. What you really want is just the asset compiled filename. If you've put the javascript files in vendor/assets/javascripts then add them to the precompile list

# in environments/production.rb
config.assets.precompile += %w( galleria.classic.min.js )

then I think you can include it like this:

Galleria.loadTheme('/assets/galleria.classic.min.js');

You may need to use a different path helper to get the digest version if the non-digest version is not available. I should look something like:

Galleria.loadTheme('<%= asset_path 'galleria.classic.min.js' %>');

Good luck. Galleria is a nice slideshow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM