javascript_include_tag 在 application.html.erb 中的 Rails 3.1 问题 [英] Rails 3.1 issue with javascript_include_tag in application.html.erb
问题描述
我创建了一个名为 demo1 的干净简单的 Rails 3.1 应用程序.它包含两个控制器 - 你好和再见.当我尝试加载 http://localhost:3000/say/hello 时出现以下错误:
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:
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'
application.html.erb 的问题行是:
<%= javascript_include_tag "application" %>
当我使用 Rails 3.0 创建应用程序时,这一行是:
When I created the application using Rails 3.0 this particular line was:
<%= javascript_include_tag :defaults %>
这工作得很好.事实上,当我将 application.html.erb 更改为使用 :defaults 时一切正常,但我想使用 Rails 3.1 的新功能.
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.
我似乎无法在 Google 上找到关于此的任何信息,我猜是因为 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.
顺便说一下,我正在学习 Agile Web Development with Rails (第 4 版)针对 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.1 book.
一些可能有助于回答这个问题的环境信息:
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
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/
推荐答案
好的,我已经弄清楚问题是什么,所以我来回答我自己的问题.
Ok, I've figured out what the issue was, and so I'll answer my own question.
问题在于 app/assets/javascripts/application.js 文件包含注释掉的代码.但是,注释掉的行之一如下:
The problem was the app/assets/javascripts/application.js file contained commented out code. However, one of the commented out lines was as follows:
//= 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.
这篇关于javascript_include_tag 在 application.html.erb 中的 Rails 3.1 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!