简体   繁体   English

ruby on rails - 添加javascript

[英]ruby on rails - add javascript

I'm a newbie ruby on rails developer.我是 Rails 开发人员的新手 ruby​​。 I've just created my first application and made use of the the bootstrap carousel.我刚刚创建了我的第一个应用程序并使用了bootstrap carousel。 Now, I want to add some javascript .现在,我想添加一些javascript My application does not have any custom javascript at this point.我的应用程序此时没有任何自定义javascript What I want to do is when the left or right button on the carousel is pressed, I want an alert to pop up.我想要做的是当按下旋转木马上的leftright按钮时,我想要弹出alert Preferably, I want to include my javascript file in the /assets/javascript directory.最好,我想将我的javascript文件包含在/assets/javascript目录中。 The purpose of this is to just learn how to add new javascript files in my application.这样做的目的是学习如何在我的应用程序中添加新的javascript文件。

Adding your custom javascript to your rails app is real easy, just follow the steps below :-将您的自定义 javascript 添加到您的 rails 应用程序非常简单,只需按照以下步骤操作:-

  • create a file (say custom.js) under app/assets/javascripts/app/assets/javascripts/下创建一个文件(比如 custom.js)
  • Open up your manifest file ie application.js (this is the file where you need to include every javascript file you want to load into your rails app, add the script name to it //=require custom .打开您的清单文件,即application.js (这是您需要包含要加载到 Rails 应用程序中的每个 javascript 文件的文件,向其中添加脚本名称//=require custom
  • Visit your rails app from browser and check the page source and search for custom.js being loaded or not thats all.从浏览器访问您的 rails 应用程序并检查页面源并搜索正在加载的 custom.js 或仅此而已。

For Rails 6对于 Rails 6

Add your javascript code under /app/javascript//file.js在 /app/javascript//file.js 下添加您的 javascript 代码

and then in app/packs/application.js然后在 app/packs/application.js

require("<folder name>/file")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM