如何将JQuery从模板添加到我的rails应用程序?

时间:2021-08-02 08:28:39

I am pretty new at this but I learned the basics of using Rails off of Codecademy and am trying to figure out how to build a basic website using a template I found.

我是新手,但我学习了使用Rails的Codecademy的基础知识,并试图找出如何使用我找到的模板构建一个基本网站。

I have been using this template: http://templated.co/retrospect and I was able to deploy almost everything but the JQuery it uses. The Rails lesson didn't cover how to add JQuery the template provides for the menu bar action (if you press the menu button, it pulls out a sidebar with menu options).

我一直在使用这个模板:http://templated.co/retrospect我几乎可以部署除了它使用的JQuery之外的所有内容。 Rails课程没有介绍如何添加模板为菜单栏操作添加的JQuery(如果按下菜单按钮,它会拉出带有菜单选项的侧栏)。

In the template files, it provides assets/js files:

在模板文件中,它提供assets / js文件:

如何将JQuery从模板添加到我的rails应用程序?

I tried putting these in my app/assets/javascripts folder of my rails app, but the menu action does not seem to be working. I checking in application.js and I do have //= require_tree . in there.

我尝试将这些放在我的rails应用程序的app / assets / javascripts文件夹中,但菜单操作似乎不起作用。我检查了application.js,我确实有// = require_tree。在那里。

Would you be able to guide me on how to make this work?

你能指导我如何做这项工作吗?

Thank you so much!

非常感谢!

Edit: I already have the 'jquery-rails' gem installed with //= require jquery //= require jquery_ujs in my application.js

编辑:我已经在我的application.js中安装了// = require jquery // = require jquery_ujs的'jquery-rails'gem

2 个解决方案

#1


0  

This might work: Use jquery as the JavaScript library

这可能有效:使用jquery作为JavaScript库

  1. Add this gem to your Gemfile gem 'jquery-rails'

    将此gem添加到您的Gemfile gem'jquery-rails'

  2. Add this to your application.js file

    将其添加到您的application.js文件中

    //= require jquery

    // =需要jquery

    //= require jquery_ujs

    // =需要jquery_ujs

This way you dont need to actually have JQuery.min.js file in assets.

这样你就不需要在资产中实际拥有JQuery.min.js文件。

#2


0  

It seems like what ended up fixing my error was using the javascript include tag for each separate file.

最终修复我的错误似乎是使用每个单独文件的javascript include标记。

Viewing the page source when it would load, the files would all import, but in one big script tag. Splitting it into different javascript include tags splits each file into separate tags which ended up working for me. I hope this can help someone!

在加载时查看页面源,文件将全部导入,但是在一个大的脚本标记中。将其拆分为不同的javascript包含标签将每个文件拆分为单独的标签,最终为我工作。我希望这可以帮助别人!

#1


0  

This might work: Use jquery as the JavaScript library

这可能有效:使用jquery作为JavaScript库

  1. Add this gem to your Gemfile gem 'jquery-rails'

    将此gem添加到您的Gemfile gem'jquery-rails'

  2. Add this to your application.js file

    将其添加到您的application.js文件中

    //= require jquery

    // =需要jquery

    //= require jquery_ujs

    // =需要jquery_ujs

This way you dont need to actually have JQuery.min.js file in assets.

这样你就不需要在资产中实际拥有JQuery.min.js文件。

#2


0  

It seems like what ended up fixing my error was using the javascript include tag for each separate file.

最终修复我的错误似乎是使用每个单独文件的javascript include标记。

Viewing the page source when it would load, the files would all import, but in one big script tag. Splitting it into different javascript include tags splits each file into separate tags which ended up working for me. I hope this can help someone!

在加载时查看页面源,文件将全部导入,但是在一个大的脚本标记中。将其拆分为不同的javascript包含标签将每个文件拆分为单独的标签,最终为我工作。我希望这可以帮助别人!