2011-09-29 46 views
0

我想学习使用JavaScript在Rails和我在http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/的Rails 3.0到3.1的JavaScript错误

下一个教程即使教程针对3.0编写,因为Rails的3.1是出我使用它,并运行进入错误。在“AJAX表单提交”部分的教程之后,它构建了一个包含以下内容的js.erb文件;

$('body').html("<h1><%= escape_javaScript(@post.title) %></h1>").append("<%=   escape_javaScript(@post.content) %>"); 

我在服务器日志中收到以下错误;

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) %>"); 

这是一个3.0到3.1的转换问题吗?有人能指引我朝着正确的方向吗?

谢谢!

回答

3

你正在寻找的方法是escape_javascript(小写字母“S”):

$('body').html("<h1><%= escape_javascript(@post.title) %></h1>").append("<%=   escape_javascript(@post.content) %>"); 

Ruby是大小写敏感的,通常使用由下划线的方法名分离小写的话。

本教程的评论可能是值得一读:

http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/comment-page-1/#comment-322477

+1

我没想到读评论,感谢您指出了这一点。当我遇到这个错误时,我想我已经很累了... – SteveO7