在Rails3.1中,js不再限定到只能用Prototype,而且改变为Unobtrusive JavaScript这种风格, Unobtrusive本意是不易察觉的,这里的意思就是不会在生成的js脚本中出现大段的代码了,而是通过一些内置的函数,动态地生成,隐式地生成,比如一个删除前的提示对话框:
<%= link_to "delete", user, :method => :delete, :confirm => "You sure?", :title => "Delete #{user.name}" %>
生成的html:
<a href="/users/14" data-confirm="You sure?" data-method="delete" rel="nofollow" title="Delete Electa Brakus">delete</a>
代码简洁干净了许多。
在需要的页面中,加入下面的代码才能工作:
<%=javascript_include_tag :application %> <%= csrf_meta_tag %>
然后,修改assests文件夹下的在assests文件夹下的applications.js,因为它默认会生成
删掉第三行,否则,提示框会弹出两次。默认是使用jquery的,当然你也可以改成别的。
此时运行如果还不好用,gemfile中加入: gem 'jquery-rails'
再重新运行:bundle install, 就应该好用了