How to require custom JS files in Rails 6

前端 未结 4 662
终归单人心
终归单人心 2020-12-14 07:52

I\'m currently trying Rails 6.0.0.rc1 which seems to have moved the default javascript folder from app/assets/javascript to app/javascript

4条回答
  •  清歌不尽
    2020-12-14 08:52

    My custom js has functions which will be called by embedded javascript of serveral html pages. Following snippet works in Rails6, compiled by webpacker:

    1. put custom js file in folder app/javascript/packs e.g. app/javascript/packs/my_functions.js

    say_hello = function(a_text){ 
        console.log("HELLO "+ a_text);  
    }

    1. add javascript_pack_tag in html file, e.g. index.html.erb .

    <%= javascript_pack_tag 'my_functions' %>
    
    
    

    Note : This line is inside html body, not in head

提交回复
热议问题