I am trying to learn to use Javascript in Rails and am following a Tutorial at http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/
Even though the tutorial is written for 3.0, since Rails 3.1 is out I am using it and running into an error. Following the lesson to the "AJAX form submission" part where it builds a js.erb file with the following content;
$('body').html("<h1><%= escape_javaScript(@post.title开发者_开发技巧) %></h1>").append("<%= escape_javaScript(@post.content) %>");
I am getting the following error in the server log;
ActionView::Template::Error (undefined method `escape_javaScript' for #<#<Class:0x00000100917048>:0x0000010084b0d8>):
1: $('body').html("<h1><%= escape_javaScript(@post.title) %></h1>").append("<%=escape_javaScript(@post.content) %>");
Is this a 3.0 to 3.1 conversion problem? Can someone point me in the right direction?
Thanks!
The method you're looking for is escape_javascript
(lower case "s"):
$('body').html("<h1><%= escape_javascript(@post.title) %></h1>").append("<%= escape_javascript(@post.content) %>");
Ruby is case sensitive and generally uses lower case words separated by underscores for method names.
The comments for the tutorial might be worth a read:
http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/comment-page-1/#comment-322477
精彩评论