Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include a Javascript file from my public folder using a wildcard character?

I'm trying to include a javascript file that's in my public folder in one of my views.

The filename includes a randomly generated string of characters, see "648eee49" below.

public/react-menu/static/js/main.648eee49.js

I've been trying to use a global directory call to return the path, but it's not working.

<%= javascript_include_tag "#{Dir.glob("/public/react-menu/static/js/main.*.js")}" %>

The file path is returning as http://localhost:3000/javascripts/[].js

like image 626
Dan G Nelson Avatar asked Dec 29 '25 19:12

Dan G Nelson


1 Answers

I was able to use Dir.glob, I just needed to remove the first forward slash from the search, and remove "public" from the path string.

<%= tag.div id: 'root' %>
<%= stylesheet_link_tag Dir.glob("public/react-menu/static/css/main.*.css")[0].sub("public", "") %>
<%= javascript_include_tag Dir.glob("public/react-menu/static/js/main.*.js")[0].sub("public", "") %>
like image 71
Dan G Nelson Avatar answered Jan 01 '26 10:01

Dan G Nelson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!