admin管理员组文章数量:1023009
I am trying to add custom JS to my Rails 7 project. I followed the instructions on this StackOverflow answer.
But I'm still not able to use custom JS in my view.
Here is my main.js:
function sayHi() {
console.log("hi");
}
window.sayHi = sayHi;
Here is my view (index.html.erb):
<div>
<button type="button" onclick="sayHi()">Click Me!</button>
</div>
When I click the button, in the console it just says:
Uncaught ReferenceError: sayHi is not defined
I'm really not sure how to access the custom JS from my view.
Here is a link to my project I uploaded to DropBox.
I am trying to add custom JS to my Rails 7 project. I followed the instructions on this StackOverflow answer.
But I'm still not able to use custom JS in my view.
Here is my main.js:
function sayHi() {
console.log("hi");
}
window.sayHi = sayHi;
Here is my view (index.html.erb):
<div>
<button type="button" onclick="sayHi()">Click Me!</button>
</div>
When I click the button, in the console it just says:
Uncaught ReferenceError: sayHi is not defined
I'm really not sure how to access the custom JS from my view.
Here is a link to my project I uploaded to DropBox.
Share Improve this question edited Dec 11, 2022 at 14:06 Slaknation asked Dec 9, 2022 at 23:29 SlaknationSlaknation 1,9364 gold badges28 silver badges48 bronze badges 1- 2 Better solution - just stop writing JS like its 2005 and attach an event handler. You'll thank me later. – max Commented Dec 11, 2022 at 8:39
3 Answers
Reset to default 4So I am going to post this as a plete answer to anyone who wants to add javascript to their Rails 7 project:
Create your custom JS file in
app/javascript/custom/main.js
(or whatever file name you like):function sayHi() { console.log("hi"); } window.sayHi = sayHi; // You must have this to expose your functions
(or you can use an event hanlder instead)
Go to
config/importmap.rb
and add the following:pin_all_from "app/javascript/custom", under: "custom"
Go to
app/javascript/application.js
file and add the following:import "custom/main"
Run In your terminal:
rails assets:prepile
Start your rails server. Voilà
I am trying to add custom JS to my Rails 7 project. I followed the instructions on this StackOverflow answer.
But I'm still not able to use custom JS in my view.
Here is my main.js:
function sayHi() { console.log("hi"); } window.sayHi = sayHi;
Here is my view (index.html.erb):
<div> <button type="button" onclick="sayHi()">Click Me!</button> </div>
When I click the button, in the console it just says:
Uncaught ReferenceError: sayHi is not defined
I'm really not sure how to access the custom JS from my view.
Here is a link to my project I uploaded to DropBox.
I am trying to add custom JS to my Rails 7 project. I followed the instructions on this StackOverflow answer.
But I'm still not able to use custom JS in my view.
Here is my main.js:
function sayHi() { console.log("hi"); } window.sayHi = sayHi;
Here is my view (index.html.erb):
<div> <button type="button" onclick="sayHi()">Click Me!</button> </div>
When I click the button, in the console it just says:
Uncaught ReferenceError: sayHi is not defined
I'm really not sure how to access the custom JS from my view.
Here is a link to my project I uploaded to DropBox.
Share Improve this question edited Dec 11, 2022 at 14:06 Slaknation asked Dec 9, 2022 at 23:29 SlaknationSlaknation 1,9364 gold badges28 silver badges48 bronze badges 1- 2 Better solution - just stop writing JS like its 2005 and attach an event handler. You'll thank me later. – max Commented Dec 11, 2022 at 8:39
3 Answers
Reset to default 4So I am going to post this as a plete answer to anyone who wants to add javascript to their Rails 7 project:
Create your custom JS file in
app/javascript/custom/main.js
(or whatever file name you like):function sayHi() { console.log("hi"); } window.sayHi = sayHi; // You must have this to expose your functions
(or you can use an event hanlder instead)
Go to
config/importmap.rb
and add the following:pin_all_from "app/javascript/custom", under: "custom"
Go to
app/javascript/application.js
file and add the following:import "custom/main"
Run In your terminal:
rails assets:prepile
Start your rails server. Voilà
本文标签: javascriptHow to use custom JS on a Rails 7 viewStack Overflow
版权声明:本文标题:javascript - How to use custom JS on a Rails 7 view? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745520660a2154300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论