javascript - How to use and load JS helpers in Stimulus controllers in Rails 7? - Stack Overflow

I want to import and use functions from my javascripthelpersfunctions.js file in my stimulus controll

I want to import and use functions from my javascript/helpers/functions.js file in my stimulus controllers.

I tried pinning it with:

# importmap.rb
pin_all_from "app/javascript/helpers", under: "helpers"

Add it to the asset prepilation with:

# asset.rb
Rails.application.config.assets.prepile += ['helpers/*']

Also I've tried to import the file in application.js in different ways (not all at once):

// These are all the ways I tried to import the helpers folder in application.js
//= require_tree helpers
import "helpers"
import "./helpers"

In my Stimulus controller, I'm importing it like this

// javascript/controllers/select_controller.js
import {show, hideAll} from "helpers/functions";

As I understand this should work as importmap is mapping it to the helpers namespace but I also tried importing it like this:

// javascript/controllers/select_controller.js
import { show, hideAll} from "../helpers/functions"

I tried all kinds of variations of the above and it works fine locally in development but anytime I run the application in production the helpers folder gets not piled or I get a 404 error.

How can I use JS helpers in Stimulus controllers the right way?

I want to import and use functions from my javascript/helpers/functions.js file in my stimulus controllers.

I tried pinning it with:

# importmap.rb
pin_all_from "app/javascript/helpers", under: "helpers"

Add it to the asset prepilation with:

# asset.rb
Rails.application.config.assets.prepile += ['helpers/*']

Also I've tried to import the file in application.js in different ways (not all at once):

// These are all the ways I tried to import the helpers folder in application.js
//= require_tree helpers
import "helpers"
import "./helpers"

In my Stimulus controller, I'm importing it like this

// javascript/controllers/select_controller.js
import {show, hideAll} from "helpers/functions";

As I understand this should work as importmap is mapping it to the helpers namespace but I also tried importing it like this:

// javascript/controllers/select_controller.js
import { show, hideAll} from "../helpers/functions"

I tried all kinds of variations of the above and it works fine locally in development but anytime I run the application in production the helpers folder gets not piled or I get a 404 error.

How can I use JS helpers in Stimulus controllers the right way?

Share Improve this question asked May 14, 2022 at 14:02 VivianaViviana 1516 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Found a solution to my own problem:

# importmap.rb
pin_all_from "app/javascript/helpers", under: "helpers"

I needed to import the specific helper file (function.js) in application.js:

// application.js
import "helpers/functions"

In the stimulus controller:

// javascript/controllers/select_controller.js
import {show, hideAll} from "helpers/functions";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745211605a4616875.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信