How to require custom JS files in Rails 6

前端 未结 4 661
终归单人心
终归单人心 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:31

    Get better-organized code and avoid multiple javascript_pack_tags in your application.html.erb file with this approach:

    1. Add your custom example.js javascript file to app/javascript/packs.
    2. Add require("packs/example") to your application.js file.

    I would have liked to add a comment to Asim Hashmi's correct answer. But I don't have enough reputation, so I'll add my suggestion as an answer instead.

    It isn't necessary to include the ".js" extension inside of the require.

提交回复
热议问题