rails:javascript

编程入门 行业动态 更新时间:2024-10-13 12:16:53
rails:javascript_include_tag呈现不正确的路径(rails: javascript_include_tag renders an incorrect path)

我正在尝试将嵌套表单放入我的某个视图中。 我已经安装了nested_form Gem并执行了安装步骤。 我做的最后一件事是编辑我的app / view / layouts / application.html.erb文件以包含此行

<%= javascript_include_tag "nested_form" %>

但是当我看着呈现的页面时,javascript不起作用。 我检查了页面源代码,发现nested_form javascript的呈现代码如下所示:

<script src="/assets/nested_form.js" type="text/javascript"></script>

所以它似乎看错了地方。 该.js文件位于public / javascripts中,不在资产中。 我做错了什么,或者我需要指定javascript_include_tag中的其他内容吗?

I am trying to put a nested form into one of my views. I have installed the nested_form Gem and performed the installation steps. The last thing I did was edit my app/view/layouts/application.html.erb file to include this line

<%= javascript_include_tag "nested_form" %>

But when I look at the rendered page, the javascript doesn't work. I examined the page source and saw that the rendered code for the nested_form javascript looks like this:

<script src="/assets/nested_form.js" type="text/javascript"></script>

So it appears to be looking in the wrong place. That .js file is located in public/javascripts, not in assets. Did I do something wrong or do I need to specify something else in that javascript_include_tag?

最满意答案

从rails 3.1开始,javascript_include_tag只会在javascripts的资产管道中查找。 如果您想将公共/ JavaScript添加到您的资产管道中,只需将公共路径添加到您的资产管道

Rails.application.config.assets.paths

数组,当你初始化你的RoR应用程序时,因为定义了资产管道看起来的路径。

所以,不,你没有做错任何事,你使用的宝石已经过时了,它是为了3.0的。 尽管我建议尝试将公共文件夹中的javascripts移动到资源文件夹中,并查看它是否可行,因为这是将javascript放入3.1应用程序的首选方式。

解决问题的另一种方法是,尽管不推荐,但完全禁用资产管道,如果你不使用它的话。 这是通过删除完成的

config.assets.enabled = true

在你的config / application.rb中

如果你想更多地了解资产管道,Railscasts上有一个视频http://railscasts.com/episodes/279-understanding-the-asset-pipeline

From rails 3.1 and onwards, javascript_include_tag will only look in the asset pipeline for javascripts. If you want to add public/javascripts into your asset pipeline, simply add the public path to your

Rails.application.config.assets.paths

array when you initialize your RoR application, since defines the paths the asset pipeline looks at.

So no you did not do anything wrong, the gem you are using is just outdated and it's for rails 3.0-. Although I recommend try moving the javascripts from the public folder to the assets folder and see if it works, because that is the preferred way to put javascripts in a 3.1 application.

Another way to solve your problem, though not recommended, is to disable the asset pipeline altogether, if you are not using it. This is done by removing

config.assets.enabled = true

in your config/application.rb

If you want to know more about the asset pipeline, Railscasts has a video on it http://railscasts.com/episodes/279-understanding-the-asset-pipeline

更多推荐

本文发布于:2023-08-01 23:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rails   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!