Ruby-on-rails javascript_include_tag 在 application.html.erb 中的 Rails 3.1 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7281907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3.1 issue with javascript_include_tag in application.html.erb
提问by ryan
I've created a clean and simple Rails 3.1 application called demo1. It contains two controllers - hello and goodbye. I get the following error when I try to load http://localhost:3000/say/hello:
我创建了一个名为 demo1 的干净简单的 Rails 3.1 应用程序。它包含两个控制器 - 你好和再见。当我尝试加载http://localhost:3000/say/hello时出现以下错误:
ActionView::Template::Error (
(in /home/me/dev/ruby/demo1/app/assets/javascripts/say.js.coffee)):
3: <head>
4: <title>Demo1</title>
5: <%= stylesheet_link_tag "application" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
app/views/layouts/application.html.erb:6:in
`_app_views_layouts_application_html_erb___558576499_89622220'
The problematic line of application.html.erbis:
application.html.erb的问题行是:
<%= javascript_include_tag "application" %>
When I created the application using Rails 3.0 this particular line was:
当我使用 Rails 3.0 创建应用程序时,这一行是:
<%= javascript_include_tag :defaults %>
And this worked fine. In fact, when I change the application.html.erb to use :defaults everything works, but I want to use the new features of Rails 3.1.
这工作得很好。事实上,当我将 application.html.erb 更改为使用 :defaults 时,一切正常,但我想使用 Rails 3.1 的新功能。
I can't seem to turn up anything on Google around this, I guess because Rails 3.1 has just been released.
我似乎无法在 Google 上找到任何关于此的信息,我猜是因为 Rails 3.1 刚刚发布。
By the way, I'm following the first chapter in the Agile Web Development with Rails (4th edition) Updated for Rails 3.1book.
顺便说一下,我正在关注使用 Rails进行敏捷 Web 开发(第 4 版)针对 Rails 3.1 更新一书的第一章。
Some environmental info that may help in answering this question:
一些可能有助于回答这个问题的环境信息:
$ cat /etc/issue
Ubuntu 10.04.2 LTS \n \l
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
$ rails -v
Rails 3.1.0
Contents of the say.js.coffeefile:
在内容say.js.coffee文件:
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
回答by ryan
Ok, I've figured out what the issue was, and so I'll answer my own question.
好的,我已经弄清楚问题是什么,所以我会回答我自己的问题。
The problem was the app/assets/javascripts/application.jsfile contained commented out code. However, one of the commented out lines was as follows:
问题是app/assets/javascripts/application.js文件包含注释掉的代码。但是,注释掉的行之一如下:
//= require_tree .
When I delete this line everything works fine.
当我删除这一行时,一切正常。
Hope this helps. If someone can provide some insight as to why the underscore was causing the issue that would be great.
希望这可以帮助。如果有人可以提供一些关于为什么下划线导致问题的见解,那就太好了。
回答by Confusion
I just ran into this issue when starting a new RoR 3.2.1 app. The problem is that you are missing a JS runtime, which is because the line
我刚刚在启动新的 RoR 3.2.1 应用程序时遇到了这个问题。问题是您缺少 JS 运行时,这是因为该行
# gem 'therubyracer'
in your Gemfile has that # in front of it. God only knows why they ship Rails with that line commented, because any tutorial that uses generate scaffoldor similar will result in the asset compilation process tripping up over the generated coffeescript file.
在你的 Gemfile 前面有 # 。天知道为什么他们发布 Rails 时注释了那行,因为任何使用generate scaffold或类似的教程都会导致资产编译过程被生成的 coffeescript 文件绊倒。
The solution is to uncomment that line. Installing another JS runtime may also solve the problem. See e.g. ExecJS and could not find a JavaScript runtime.
解决方案是取消注释该行。安装另一个 JS 运行时也可以解决问题。参见例如ExecJS 并且找不到 JavaScript 运行时。
The official ruby on rails guidesays:
该上轨导向官方红宝石说:
Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an execjs error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the therubyracer gem to Gemfile in a commented line for new apps and you can uncomment if you need it. therubyrhino is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at ExecJS.
将 CoffeeScript 编译为 JavaScript 需要一个 JavaScript 运行时,如果没有运行时,将会出现 execjs 错误。通常 Mac OS X 和 Windows 都安装了 JavaScript 运行时。Rails 在新应用程序的注释行中将 therubyracer gem 添加到 Gemfile,如果需要,您可以取消注释。therubyrhino 是 JRuby 用户推荐的运行时,默认情况下被添加到在 JRuby 下生成的应用程序中的 Gemfile。您可以在 ExecJS 上调查所有支持的运行时。
回答by Talha
Simply delete the 2 lines from application.js
只需从 application.js
//= require turbolinks
//= require turbolinks
//= require_tree
//= require_tree
回答by Knyteguy
If you're on Windows and you used RailsInstaller to get your development going, you need to use that .bat file included with RailsInstaller to run your server. Also you can't use any CMD hook like conemu to start the server, unfortunately.
如果您使用的是 Windows 并且使用 RailsInstaller 进行开发,则需要使用 RailsInstaller 附带的 .bat 文件来运行服务器。不幸的是,您也不能使用任何 CMD 挂钩(如 conemu)来启动服务器。
回答by camflint
For the benefit of future Googlers, I went down a rabbit hole trying to install therubyraceruntil I stumbled upon this related post:
为了未来的 Google 员工的利益,我在尝试安装therubyracer之前陷入了困境,直到我偶然发现了这篇相关帖子:
ExecJS::RuntimeError in rails 3.2.8 engine with javascript_include_tag
带有 javascript_include_tag 的 rails 3.2.8 引擎中的 ExecJS::RuntimeError
The link mentioned explains that ExecJS is configured incorrectly for Windows 8 out-of-the-box. In particular, the workaround described by pottsk did the trick for me.
提到的链接解释了 ExecJS 为 Windows 8 开箱即用的配置不正确。特别是,pottsk 描述的解决方法对我有用。
https://github.com/sstephenson/execjs/issues/81#issuecomment-9892952
https://github.com/sstephenson/execjs/issues/81#issuecomment-9892952
It involves changing the way that the cscriptexecutable is run by ExecJS in %rubyinstall%\gems\2.0.0\gems\execjs-2.0.1\lib\execjs\runtimes.rb:
它涉及更改cscriptExecJS 运行可执行文件的方式%rubyinstall%\gems\2.0.0\gems\execjs-2.0.1\lib\execjs\runtimes.rb:
# JScript = ExternalRuntime.new(
# :name => "JScript",
# :command => "cscript //E:jscript //Nologo //U",
# :runner_path => ExecJS.root + "/support/jscript_runner.js",
# :encoding => 'UTF-16LE' # CScript with //U returns UTF-16LE
# )
JScript = ExternalRuntime.new(
:name => "JScript",
:command => "cscript //E:jscript //Nologo",
:runner_path => ExecJS.root + "/support/jscript_runner.js",
:encoding => 'UTF-8' # CScript with //U returns UTF-16LE
)
It goes without saying that this is not a permanent solution but it does get me up and running until the patch is incorporated.
不用说,这不是一个永久的解决方案,但它确实让我开始运行,直到补丁被合并。
回答by DavB
I had the same problem running rails 3.1.1
我在运行 rails 3.1.1 时遇到了同样的问题
Once I reinstated the following code in my Gemfile, my issue disappeared:
一旦我在 Gemfile 中恢复了以下代码,我的问题就消失了:
group :assets do
gem 'sass-rails', '~> 3.1.4'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
This was placed in my Gemfile when initially setting up the project, i.e. using "rails new myapp".
这在最初设置项目时放置在我的 Gemfile 中,即使用“rails new myapp”。
Note that the in my template file I include:
请注意,在我的模板文件中,我包括:
<%= javascript_include_tag :application %>
Hope that helps
希望有帮助
回答by thedanotto
Removing //= require_treewill just stop your javascript files from being loaded into rails. It will probably stop the error, but probablynot what you'd like.
删除//= require_tree只会阻止您的 javascript 文件加载到 rails 中。它可能会停止错误,但可能不是您想要的。
However, in my case, I had some .js.coffeefiles with incorrect indenting. Once I fixed this (by deleting them), it worked.
但是,就我而言,我有一些.js.coffee缩进不正确的文件。一旦我解决了这个问题(通过删除它们),它就起作用了。
回答by Tarun Reddy
Adding the gem 'therubyracer' fixed the issue. I was getting the error only after adding external js file through <%= javascript_include_tag %>
添加 gem 'therubyracer' 解决了这个问题。仅在通过 <%= javascript_include_tag %> 添加外部 js 文件后才收到错误
回答by Gautam
devise_scope :user do
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
If your routes.rb has this above line just delete it and run. This is what corrected my problem.
如果你的 routes.rb 上面有这行,就删除它并运行。这就是纠正我的问题。
回答by kys48
modify generator file (application.html.erb.tt) as the following:
修改生成器文件 ( application.html.erb.tt) 如下:
<%= javascript_include_tag "application", "data-turbolinks-track" true %>
<%= javascript_include_tag :default, "data-turbolinks-track" true %>

