1. 创建文件:lib/tasks/rails.rake
desc "Checks your app and gently warns you if you are using deprecated code."
task :deprecated => :environment do
deprecated = {
'@params' => 'Use params[] instead',
'@session' => 'Use session[] instead',
'@flash' => 'Use flash[] instead',
'@request' => 'Use request[] instead',
'@env' => 'Use env[] instead',
'find_all' => 'Use find(:all) instead',
'find_first' => 'Use find(:first) instead',
'render_partial' => 'Use render :partial instead',
'component' => 'Use of components are frowned upon',
'paginate' => 'The default paginator is slow. Writing your own may be faster',
'start_form_tag' => 'Use form_for instead',
'end_form_tag' => 'Use form_for instead',
':post => true' => 'Use :method => :post instead'
}
deprecated.each do |key, warning|
puts '--> ' + key
output = `cd '#{File.expand_path('app', RAILS_ROOT)}' && grep -n --exclude=*.svn* -r '#{key}' *`
unless output =~ /^$/
puts " !! " + warning + " !!"
puts ' ' + '.' * (warning.length + 6)
puts output
else
puts " Clean! Cheers for you!"
end
puts
end
end
2. 执行 #railsapp>rake deprecated
可以查看升级所需要更改的。即使现在不马上升级,到是可以随时注意一些写法。
3.使用插件 willpaginate
# install
#railsapp>ruby /script/plugin install svn://errtheblog.com/svn/plugins/will_paginate
# 替换以前的代码
@skus_pages,@skus = paginate :sku, :conditions => sql , :per_page => 30 ,
rder => "skuid"
替换为:
@skus = Sku.paginate :conditions => sql , :per_page => 30 ,
rder => "skuid",:page=>params["page"]
记得要增加:page=>params["page"],以前是不用的。
可能你会发现@skus_pages不见了,不用担心,它已经集成到了@skus中了
pages.current.previous => objs.previous_page
pages.current_page.number => objs.current_page
pages.current.next => objs.next_page
没有了pages.last , 但是可以考虑使用 objs.page_count
没有了pages.first , 但是可以考虑直接使用 1
willpaginate 多执行了一次统计总记录条数的sql语句,所以可以直接使用objs.total_entries 获取总记录数量。
参考文档:
http://www.slashdotdash.net/articles/2007/12/03/rails-2-upgrade-notes
http://errtheblog.com/posts/56-im-paginating-again
http://www.iteye.com/news/567