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");
}
});
the coffeescript isn’t coffeescript
should be: