使用 asset .js.coffee 文件时出现 RuntimeError
我刚刚开始使用 Rails 3.1 和 jQuery / Coffee 脚本。我有一段 js 代码,当包含在我视图中的标签中时可以工作,但是当包含在 app/assets/javascripts/post.js.coffee 中时它会抛出以下错误:
Posts#new 中的 ExecJS::RuntimeError
显示中 /home/chris/RailsDev/blog/app/views/layouts/application.html.erb 其中 第 10 行升高:
第 7 行保留字“function”(在 /home/chris/RailsDev/blog/app/assets/javascripts/post.js.coffee)
这有效:
app/views/posts/new.html.erb
<%= form_for [@user, @post] do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<label for="forward_date">Post in the future?</label>
<%= check_box_tag 'forward date' %>
<div id="post_date" style="display: none;">
<%= f.label :post_date %>
<%= f.datetime_select :post_date %>
</div>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
<script>
$("#forward_date").change(function() {
if($(this).is(":checked")) {
$("#post_date").show("slow");
} else {
$("#post_date").hide("slow");
}
});
</script>
这会抛出 ExecJS:: RuntimeError
从视图中删除标签并将代码放在 app/assets/javascripts/post.js.coffee 中
$("#forward_date").change(function() {
if($(this).is(":checked")) {
$("#post_date").show("slow");
} else {
$("#post_date").hide("slow");
}
});
I'm just getting started using rails 3.1 and jQuery / coffee scripts. I have a piece of js code which works when included in a tag in my view but when included in app/assets/javascripts/post.js.coffee it throws the following error:
ExecJS::RuntimeError in Posts#new
Showing
/home/chris/RailsDev/blog/app/views/layouts/application.html.erb where
line #10 raised:Reserved word "function" on line 7 (in
/home/chris/RailsDev/blog/app/assets/javascripts/post.js.coffee)
This works:
app/views/posts/new.html.erb
<%= form_for [@user, @post] do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<label for="forward_date">Post in the future?</label>
<%= check_box_tag 'forward date' %>
<div id="post_date" style="display: none;">
<%= f.label :post_date %>
<%= f.datetime_select :post_date %>
</div>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
<script>
$("#forward_date").change(function() {
if($(this).is(":checked")) {
$("#post_date").show("slow");
} else {
$("#post_date").hide("slow");
}
});
</script>
This throws ExecJS::RuntimeError
Remove the tag from the view and place the code in app/assets/javascripts/post.js.coffee
$("#forward_date").change(function() {
if($(this).is(":checked")) {
$("#post_date").show("slow");
} else {
$("#post_date").hide("slow");
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
咖啡脚本不是咖啡脚本
应该是:
the coffeescript isn't coffeescript
should be:
这不是在 Coffeescript 中声明函数的方式。使用
->
而不是关键字function
:That is not how you declare a function in Coffeescript. Instead of the keyword
function
use->
: