<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LetRails</title>
	<atom:link href="http://www.letrails.cn/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.letrails.cn</link>
	<description>为Ruby on Rails在中文社区的枝繁叶茂贡献点滴</description>
	<lastBuildDate>Tue, 31 Aug 2010 08:25:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>抢答开源</title>
		<link>http://www.letrails.cn/archives/qiangda-is-open-source-now/</link>
		<comments>http://www.letrails.cn/archives/qiangda-is-open-source-now/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 08:25:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[其它]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=244</guid>
		<description><![CDATA[抢答本身是抢座的表单模块，但是由于Google Form在国内无法正常使用，所有我们将其剥离了出来，作为一个单独的产品运营，经过这段时间的运营，我们收到了许多用户反馈，其中大部分是功能需求，但是由于我们的人力有限，这些功能一直没有做，所以我们决定将抢答的代码开源出来，希望能够借助社区的力量让这个产品更加的完善，也希望能够对需要此功能的朋友有所帮助。 源码地址： http://github.com/yzhang/thundersurvey 许可证： GPLv2 抢答是基于Rails 3和MongoDB，因此你需要首先安装mongodb，然后执行下面的命令即可： $ git clone http://github.com/yzhang/thundersurvey $ bundle install $ sudo mongod $ rails s 如果有问题或者建议，欢迎和我联系。]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.51qiangda.com">抢答</a>本身是<a href="http://www.51qiangzuo.com">抢座</a>的表单模块，但是由于Google Form在国内无法正常使用，所有我们将其剥离了出来，作为一个单独的产品运营，经过这段时间的运营，我们收到了许多用户反馈，其中大部分是功能需求，但是由于我们的人力有限，这些功能一直没有做，所以我们决定将抢答的代码开源出来，希望能够借助社区的力量让这个产品更加的完善，也希望能够对需要此功能的朋友有所帮助。</p>
<p>源码地址： <a href="http://github.com/yzhang/thundersurvey">http://github.com/yzhang/thundersurvey</a><br />
许可证： GPLv2</p>
<p>抢答是基于Rails 3和MongoDB，因此你需要首先安装mongodb，然后执行下面的命令即可：</p>
<p>$ git clone http://github.com/yzhang/thundersurvey<br />
$ bundle install<br />
$ sudo mongod<br />
$ rails s</p>
<p>如果有问题或者建议，欢迎和我联系。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/qiangda-is-open-source-now/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Rails 图表解决方案汇总</title>
		<link>http://www.letrails.cn/archives/rails-chart-solutions/</link>
		<comments>http://www.letrails.cn/archives/rails-chart-solutions/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 12:03:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=238</guid>
		<description><![CDATA[最近需要给抢座增加图表功能，于是对Rails的图表方案做了些调查，整理成这篇贴，希望对有同样需求的人有所帮助。 Google Chart Google Chart的优点是接口简单，容易上手，但是缺点也很明显，需要依赖Google的服务，在有些地方加载可能会存在速度问题，并且有被GFW的风险。 Google Chart的Rails插件很多，个人比较喜欢mattetti的googlecharts： googlecharts: http://github.com/mattetti/googlecharts gchartrb：http://code.google.com/p/gchartrb google-charts-on-rails：http://code.google.com/p/google-charts-on-rails/ gc4r：http://code.google.com/p/gc4r/ Google Visualization Google Visualization是Google Chart的升级版，Rails插件：http://github.com/mwarnock/rails-google-visualization-plugin Gruff Gruff是基于RMagick的Ruby绘图工具，功能强大，可扩展性和可定制性都非常好，不过由于Gruff需要在服务端实时生成图片，因此不适合实时展示数据。 Gruff还有一个JS封装bluff：http://bluff.jcoglan.com/ Flot Flot是一个jQuery插件，可以基于JS生成实时图表，如果你使用jQuery，那么Flot是个不错的选择。Flot有一个很好用的Rails插件插件Flotilla: http://github.com/joshuamiller/flotilla GNUPlot GNUPlot最初是为学术目的使用的数据可视化工具，但是现在也支持生成非交互式的2d和3d图形，如果你需要生成非常复杂的图形，那么可以试试gnuplot，GNUPlot有一个叫rgplot的rubygem可以使用：http://rgplot.rubyforge.org Ziya Ziya是一个Rails图表插件，可以生成非常漂亮和复杂的Flash图表，如果你不介意Flash，那么Ziya是个不错的选择。 Scruffy Scruffy是一个Rubygem，它的目标是帮你生成高质量的SVG和PNG图片，同Gruff一样，是一个服务端解决方案，不过Scruffy目前似乎已经停止开发了，首页的图片在Firefox和Chrome下均无法显示。 Highcharts 感谢dxiao同学留言，Highchars是一个纯JS的图表库，但是实现的效果却和Flash不相上下，相当绚丽，兼容性也很不错，并且有一个Rails的插件可以用，值得一试。 Flash 除了上面介绍的Ziya，Flash图表解决方案还有很多，下面列举几个对Rails支持比较好的： Open Flash Chart II：http://pullmonkey.com/projects/open_flash_chart2/ Fusion Charts Free：http://www.fusioncharts.com/free/ maani.us xml/swf charts：http://www.maani.us/xml_charts/ amcharts： http://www.amcharts.com/，archarts有一个很好用的Rails插件：ambling 参考：http://stackoverflow.com/questions/87561/what-is-your-preferred-way-to-produce-charts-in-a-ruby-on-rails-web-application]]></description>
			<content:encoded><![CDATA[<p>最近需要给<a href="http://www.51qiangzuo.com/">抢座</a>增加图表功能，于是对Rails的图表方案做了些调查，整理成这篇贴，希望对有同样需求的人有所帮助。</p>
<p><a href="http://code.google.com/apis/chart/">Google Chart</a></p>
<p>Google Chart的优点是接口简单，容易上手，但是缺点也很明显，需要依赖Google的服务，在有些地方加载可能会存在速度问题，并且有被GFW的风险。</p>
<p>Google Chart的Rails插件很多，个人比较喜欢mattetti的googlecharts：</p>
<ul>
<li>googlecharts: <a href="googlecharts:  github.com/mattetti/googlecharts"> http://github.com/mattetti/googlecharts</a></li>
<li>gchartrb：<a href="http://code.google.com/p/gchartrb">http://code.google.com/p/gchartrb</a></li>
<li>google-charts-on-rails：<a href="http://code.google.com/p/google-charts-on-rails/">http://code.google.com/p/google-charts-on-rails/</a>
</li>
<li>gc4r：<a href="http://code.google.com/p/google-charts-on-rails/">http://code.google.com/p/gc4r/</a></li>
</ul>
<p><a href="http://code.google.com/apis/visualization">Google Visualization</a></p>
<p>Google Visualization是Google Chart的升级版，Rails插件：<a href="http://github.com/mwarnock/rails-google-visualization-plugin">http://github.com/mwarnock/rails-google-visualization-plugin</a></p>
<p><a href="http://nubyonrails.com/pages/gruff">Gruff</a></p>
<p>Gruff是基于RMagick的Ruby绘图工具，功能强大，可扩展性和可定制性都非常好，不过由于Gruff需要在服务端实时生成图片，因此不适合实时展示数据。</p>
<p>Gruff还有一个JS封装bluff：<a href="http://bluff.jcoglan.com/">http://bluff.jcoglan.com/</a></p>
<p><a href="http://code.google.com/p/flot/">Flot</a></p>
<p>Flot是一个jQuery插件，可以基于JS生成实时图表，如果你使用jQuery，那么Flot是个不错的选择。Flot有一个很好用的Rails插件插件Flotilla: <a href="http://github.com/joshuamiller/flotilla">http://github.com/joshuamiller/flotilla</a></p>
<p><a href="http://www.gnuplot.info/">GNUPlot</a></p>
<p>GNUPlot最初是为学术目的使用的数据可视化工具，但是现在也支持生成非交互式的2d和3d图形，如果你需要生成非常复杂的图形，那么可以试试gnuplot，GNUPlot有一个叫rgplot的rubygem可以使用：<a href="http://rgplot.rubyforge.org">http://rgplot.rubyforge.org</a></p>
<p><a href="http://ziya.liquidrail.com/">Ziya</a></p>
<p>Ziya是一个Rails图表插件，可以生成非常漂亮和复杂的Flash图表，如果你不介意Flash，那么Ziya是个不错的选择。</p>
<p><a href="http://scruffy.rubyforge.org/">Scruffy</a></p>
<p>Scruffy是一个Rubygem，它的目标是帮你生成高质量的SVG和PNG图片，同Gruff一样，是一个服务端解决方案，不过Scruffy目前似乎已经停止开发了，首页的图片在Firefox和Chrome下均无法显示。</p>
<p>Highcharts</p>
<p>感谢dxiao同学留言，<a href="http://www.highcharts.com/">Highchars</a>是一个纯JS的图表库，但是实现的效果却和Flash不相上下，相当绚丽，兼容性也很不错，并且有一个<a href="http://github.com/loudpixel/highcharts-rails">Rails的插件</a>可以用，值得一试。</p>
<p>Flash</p>
<p>除了上面介绍的Ziya，Flash图表解决方案还有很多，下面列举几个对Rails支持比较好的：</p>
<ul>
<li>Open Flash Chart II：<a href="http://pullmonkey.com/projects/open_flash_chart2/">http://pullmonkey.com/projects/open_flash_chart2/</a></li>
<li>Fusion Charts Free：<a href="http://www.fusioncharts.com/free/">http://www.fusioncharts.com/free/</a></li>
<li>maani.us xml/swf charts：<a href="http://www.maani.us/xml_charts/">http://www.maani.us/xml_charts/</a></li>
<li>amcharts： <a href="http://www.amcharts.com/">http://www.amcharts.com/</a>，archarts有一个很好用的Rails插件：<a href="http://ambling.rubyforge.org/">ambling</a></li>
</ul>
<p>参考：<a href="http://stackoverflow.com/questions/87561/what-is-your-preferred-way-to-produce-charts-in-a-ruby-on-rails-web-application">http://stackoverflow.com/questions/87561/what-is-your-preferred-way-to-produce-charts-in-a-ruby-on-rails-web-application</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/rails-chart-solutions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>使用map/reduce为mongo_mapper增加sum方法</title>
		<link>http://www.letrails.cn/archives/add-sum-method-for-mongo-mapper/</link>
		<comments>http://www.letrails.cn/archives/add-sum-method-for-mongo-mapper/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 04:20:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[其它]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=233</guid>
		<description><![CDATA[mongodb的map/reduce是个十分强大的功能，mongo_mapper虽然已经接近于1.0，但还是少一些比较有用的功能，比如对一个字段求和，取最大，最小值等，不过你可以很容易的通过mongodb的map/reduce为mongo_mapper扩展这些功能，甚至是更复杂的，下面以sum为例： class Model &#160;&#160;def self.sum(field) &#160;&#160;&#160;&#160;m = "function () {emit('sum', this.#{field.to_s})}" &#160;&#160;&#160;&#160;r = "function(k, vals) { var sum = 0; for(var i in vals){sum += vals[i];}; return sum;}" &#160;&#160;&#160;&#160;res = self.collection.map_reduce(m, r) &#160;&#160;&#160;&#160;return res.find().next_document['value'].to_i &#160;&#160;end end]]></description>
			<content:encoded><![CDATA[<p>mongodb的<a href="http://www.mongodb.org/display/DOCS/MapReduce">map/reduce</a>是个十分强大的功能，mongo_mapper虽然已经接近于1.0，但还是少一些比较有用的功能，比如对一个字段求和，取最大，最小值等，不过你可以很容易的通过mongodb的map/reduce为mongo_mapper扩展这些功能，甚至是更复杂的，下面以sum为例：</p>
<p class="code"><code class="ruby"><br />
class Model<br />
&nbsp;&nbsp;def self.sum(field)<br />
&nbsp;&nbsp;&nbsp;&nbsp;m = "function () {emit('sum', this.#{field.to_s})}"<br />
&nbsp;&nbsp;&nbsp;&nbsp;r =   "function(k, vals) { var sum = 0; for(var i in vals){sum += vals[i];}; return sum;}"<br />
&nbsp;&nbsp;&nbsp;&nbsp;res = self.collection.map_reduce(m, r)<br />
&nbsp;&nbsp;&nbsp;&nbsp;return res.find().next_document['value'].to_i<br />
&nbsp;&nbsp;end<br />
end</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/add-sum-method-for-mongo-mapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails性能优化简明指南</title>
		<link>http://www.letrails.cn/archives/rails-performance-optimization-guide/</link>
		<comments>http://www.letrails.cn/archives/rails-performance-optimization-guide/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 12:35:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[性能]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=215</guid>
		<description><![CDATA[最近多背一公斤的网站由于前段时间加了太多功能，但是没有考虑性能问题，导致速度下降，内存占用剧增，apache频频重启，需要进行优化，于是花了点时间学习了下rails的优化，整理成这篇文章，希望对遇到同样问题的人有用。 基本原则 大部分讲优化的文章都会在开头讲到优化的基本原则： 1. 不要过早优化，这里必须要提到Donald Knuth的名言：过早的优化是万恶之源，Premature optimization is the root of all evil，在你的应用还没有遇到性能问题之前，不要为性能担忧，也无需为优化浪费时间，只有当性能成为问题时才进行优化，这是第一原则。 2. 基准测试是必须的，没有基准测试，优化的效果就无从衡量，所有优化的第一步都应该是基准测试。 3. 要适可而止，不要追求极致。性能的优化实际上是个没有尽头的事情，没有任何一个应用可以在性能方面达到完美，只要你愿意，总是能找到可以优化的地方，但是这里要进行权衡，如果你的团队花了1个月的时间进行优化，而性能仅仅提高了10%，那么不如添置一台服务器，一台服务器的成本肯定要远远小于你的团队一个月的工资，另外，也没有必要为了一些可有可无的优化而牺牲代码的可读性，什么是可有可无的优化？比如直接写a标签肯定比link_to要快，调用 helper肯定也不如将所有代码写在view中快，但这样的优化得不偿失，没有意义。 看过了这些原则，如果你还认为你的应用需要优化，那就接着往下看吧！ request-log-analytizer 优化的第一个工作应该是找出问题所在，我要介绍的第一个工具request-log-analytizer，就是为这个工作准备的，这是一个日志分析工具，它可以通过对日志进行分析，找出应用的瓶颈所在，request-log-analytizer的好处就是所有的分析都是基于生产环境，避免了做无用功。 request-log-analytizer的使用非常简单： sudo gem install request-log-analytizer request-log-analytizer log/production.log 现在你就可以在命令行看到日志分析结果了，为了方便查看，request-log-analytizer也支持以HTML格式输出结果： request-log-analytizer -f log.html &#8211;output html log/production.log 用浏览器打开log.html，可以看到最上面是摘要，告诉你日志中总共有多少个请求，以及请求的时间跨度，下面一个部分是每个时间段的平均请求数，然后就是被请求次数最多的20个URL的请求信息，接下来是按照请求方法和响应分类的请求信息。 在这些信息的后面，就是我们关心的性能信息了，这里有6块，总处理时间最长的请求，平均处理时间最长的请求，总View渲染时间最长的请求，平均View渲染时间最长的请求，总数据库查询时间最长的请求以及平均数据库查询时间最长的请求，通过这6个部分，我们就可以了解到应用的性能瓶颈，在这6块后面，request-log-analytizer还列出了平均处理时间超过1秒的请求，这些应该是优化的重点，如下图，我们可以看出，1公斤的网站需要优化的请求是如此之多。 有了目标，下面就让我们来挨个处理它们。 Benchmark 在开始优化之前，还有一个工作就是基准测试，就像前面的优化准则提到的，只有做了基准测试，我们才能判断经过我们的优化，性能到底有没有提高，提高了多少？ Rails 从2.2开始内置了Benchmarker和Profiler工具，如果你新建一个Rails应用，在script/performance目录下就可以看到它们，你也可以通过performance_test这个generator来生成新的性能测试用例，关于Rails的性能测试，请参考Rails的官方指南（http://guides.rubyonrails.org/performance_testing.html），介绍非常详细，但是这里我们要解决的是生产环境的问题，测试环境很难模拟出生产环境中的数据，所以这里我们需要用到rails的benchmarker脚本： $ RAILS_ENV=production ruby script/performance/benchmarker 4 "ActionController::Integration::Session.new.get_via_redirect('/activities')" user system total real #1 2.720000 0.040000 [...]]]></description>
			<content:encoded><![CDATA[<p>最近<a href="http://www.1kg.org">多背一公斤</a>的网站由于前段时间加了太多功能，但是没有考虑性能问题，导致速度下降，内存占用剧增，apache频频重启，需要进行优化，于是花了点时间学习了下rails的优化，整理成这篇文章，希望对遇到同样问题的人有用。</p>
<p>基本原则</p>
<p>大部分讲优化的文章都会在开头讲到优化的基本原则：</p>
<p>1. 不要过早优化，这里必须要提到<a href="http://en.wikiquote.org/wiki/Donald_Knuth">Donald Knuth</a>的名言：过早的优化是万恶之源，Premature optimization is the root of all evil，在你的应用还没有遇到性能问题之前，不要为性能担忧，也无需为优化浪费时间，只有当性能成为问题时才进行优化，这是第一原则。</p>
<p>2. 基准测试是必须的，没有基准测试，优化的效果就无从衡量，所有优化的第一步都应该是基准测试。</p>
<p>3. 要适可而止，不要追求极致。性能的优化实际上是个没有尽头的事情，没有任何一个应用可以在性能方面达到完美，只要你愿意，总是能找到可以优化的地方，但是这里要进行权衡，如果你的团队花了1个月的时间进行优化，而性能仅仅提高了10%，那么不如添置一台服务器，一台服务器的成本肯定要远远小于你的团队一个月的工资，另外，也没有必要为了一些可有可无的优化而牺牲代码的可读性，什么是可有可无的优化？比如直接写a标签肯定比link_to要快，调用 helper肯定也不如将所有代码写在view中快，但这样的优化得不偿失，没有意义。</p>
<p>看过了这些原则，如果你还认为你的应用需要优化，那就接着往下看吧！</p>
<p>request-log-analytizer</p>
<p>优化的第一个工作应该是找出问题所在，我要介绍的第一个工具<a href="http://github.com/wvanbergen/request-log-analyzer">request-log-analytizer</a>，就是为这个工作准备的，这是一个日志分析工具，它可以通过对日志进行分析，找出应用的瓶颈所在，request-log-analytizer的好处就是所有的分析都是基于生产环境，避免了做无用功。</p>
<p>request-log-analytizer的使用非常简单：</p>
<p class="code"><code class="ruby"><br />
sudo gem install request-log-analytizer<br />
request-log-analytizer log/production.log<br />
</code></p>
<p>现在你就可以在命令行看到日志分析结果了，为了方便查看，request-log-analytizer也支持以HTML格式输出结果：</p>
<p>request-log-analytizer -f log.html &#8211;output html log/production.log</p>
<p>用浏览器打开log.html，可以看到最上面是摘要，告诉你日志中总共有多少个请求，以及请求的时间跨度，下面一个部分是每个时间段的平均请求数，然后就是被请求次数最多的20个URL的请求信息，接下来是按照请求方法和响应分类的请求信息。</p>
<p>在这些信息的后面，就是我们关心的性能信息了，这里有6块，总处理时间最长的请求，平均处理时间最长的请求，总View渲染时间最长的请求，平均View渲染时间最长的请求，总数据库查询时间最长的请求以及平均数据库查询时间最长的请求，通过这6个部分，我们就可以了解到应用的性能瓶颈，在这6块后面，request-log-analytizer还列出了平均处理时间超过1秒的请求，这些应该是优化的重点，如下图，我们可以看出，1公斤的网站需要优化的请求是如此之多。</p>
<p><a href="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-6.51.30-PM.png"><img src="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-6.51.30-PM-300x206.png" alt="" title="Screen shot 2010-07-21 at 6.51.30 PM" width="300" height="206" class="alignnone size-medium wp-image-224" /></a></p>
<p>有了目标，下面就让我们来挨个处理它们。</p>
<p>Benchmark</p>
<p>在开始优化之前，还有一个工作就是基准测试，就像前面的优化准则提到的，只有做了基准测试，我们才能判断经过我们的优化，性能到底有没有提高，提高了多少？</p>
<p>Rails 从2.2开始内置了Benchmarker和Profiler工具，如果你新建一个Rails应用，在script/performance目录下就可以看到它们，你也可以通过performance_test这个generator来生成新的性能测试用例，关于Rails的性能测试，请参考Rails的官方指南（http://guides.rubyonrails.org/performance_testing.html），介绍非常详细，但是这里我们要解决的是生产环境的问题，测试环境很难模拟出生产环境中的数据，所以这里我们需要用到rails的benchmarker脚本：</p>
<p class="code"><code class="ruby"><br />
$ RAILS_ENV=production ruby script/performance/benchmarker 4 "ActionController::Integration::Session.new.get_via_redirect('/activities')"<br />
          user     system      total        real<br />
#1      2.720000   0.040000   2.760000 (  4.861908)<br />
</code></p>
<p>这条命令需要说明一下，首先我们设置benchmarker运行在production环境，第一个参数4是告诉benchmarker脚本的运行次数，第二个参数就是我们的测试，ActionController::Integration::Session是Rails提供的集成测试框架，可以用来模拟用户请求，这里我们看到，未优化前，处理4个/activities请求花去了2.76秒的时间，现在我们就有了一个判断的标准，后面我们就可以通过这条命令来检验我们的优化提高了多少性能。</p>
<p>不过不幸的是，Rails自从2.3.4版本开始，引入了一个Bug，导致这条命令不能在Mac系统运行，如果你使用的是mac系统，可以将Rails降回2.3.4以下版本，或者直接在console运行：</p>
<p class="code"><code class="ruby"><br />
Benchmark.bm do |x|<br />
&nbsp;&nbsp;x.report { 4.times { app.get(“/activities”) }}<br />
end<br />
</code></p>
<p>除了benchmarker，rails还内置了一个profiler脚本，是对ruby-prof的封装，你也可以用这个工具对你的action进行profiler测试，看看到底是那个函数调用浪费了时间：</p>
<p class="code"><code class="ruby"><br />
$ RAILS_ENV=production ruby script/performance/profiler "ActionController::Integration::Session.new.get_via_redirect('/activities')"<br />
</code></p>
<p>query_reviewer</p>
<p>有了优化基准，下面就让我们以/activities为例，来看看应该如何进行优化，通过request-log-analytizer我们看到，这个请求的花销主要在数据库上，所以优化的关键就是减少和优化数据库查询，<a href="http://github.com/dsboulder/query_reviewer">query_reviewer</a>就是为这个工作准备的，它实际上是mysql的 explain命令的封装，query_reviewer的安装非常简单，安装完成后，会在页面的左上角出现一个小的方框，点击就会显示当前页面的SQL 查询需要进行哪些优化，下面是1公斤的/activities页面的输出：</p>
<p><a href="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-7.19.38-PM.png"><img src="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-7.19.38-PM-300x188.png" alt="" title="Screen shot 2010-07-21 at 7.19.38 PM" width="300" height="188" class="alignnone size-medium wp-image-225" /></a></p>
<p>从上图可以看出，这个请求进行了65次数据库查询，并且存在18个错误，经过对这些查询进行优化以及增加counter cache后，成功的将查询减到了8条，时间也从0.534减到了0.012，速度提高了44倍：</p>
<p><a href="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-8.01.04-PM.png"><img src="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-8.01.04-PM-300x202.png" alt="" title="Screen shot 2010-07-21 at 8.01.04 PM" width="300" height="202" class="alignnone size-medium wp-image-226" /></a></p>
<p>bullet</p>
<p>这里还要顺便介绍一下国人<a href="http://github.com/flyerhzm/bullet">flyerhzm开发的bullet插件</a>，这是一个用于检测eager loading的插件，可以有效减少SQL查询的数量，安装成功后，它会在有问题的页面给出下图这样的提示：</p>
<p><a href="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-7.56.32-PM.png"><img src="http://www.letrails.cn/wp-content/uploads/2010/07/Screen-shot-2010-07-21-at-7.56.32-PM-300x104.png" alt="" title="Screen shot 2010-07-21 at 7.56.32 PM" width="300" height="104" class="alignnone size-medium wp-image-227" /></a></p>
<p>上图告诉你应该在调用Photo的find方法时包含:activity对象，这样就不用为每个photo对象再单独查询一次activity了。</p>
<p>好了，经过上面的优化，让我们再来运行benchmarker检验一下效果吧：</p>
<p class="code"><code class="ruby"><br />
$ RAILS_ENV=production ruby script/performance/benchmarker 4 "ActionController::Integration::Session.new.get_via_redirect('/activities')"<br />
           user     system      total        real<br />
#1      0.680000   0.000000   0.680000 (  0.696291)<br />
</code></p>
<p>可以看到，经过这番优化，对/activities页面的请求从2.76秒降到了0.68秒，提高了超过2秒，单个请求提高了0.5秒，任务完成。</p>
<p>不过如果你的问题在于view渲染过慢，或者经过上面的优化，数据库的性能依然不能令人满意，那你可能就需要增加页面缓存，或者是借助大名鼎鼎的memcached了，关于这两个的介绍可以参看下面的文章：</p>
<p>Rails缓存：<a href="http://railsenvy.com/2007/2/28/rails-caching-tutorial">http://railsenvy.com/2007/2/28/rails-caching-tutorial</a><br />
Rails Memcached：<a href="http://nubyonrails.com/articles/memcached-basics-for-rails">http://nubyonrails.com/articles/memcached-basics-for-rails</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/rails-performance-optimization-guide/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>用logrotate自动打包Rails的日志</title>
		<link>http://www.letrails.cn/archives/rotate-rails-log-with-logrotate/</link>
		<comments>http://www.letrails.cn/archives/rotate-rails-log-with-logrotate/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 03:18:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[其它]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=218</guid>
		<description><![CDATA[logrotate是Linux系统的日志打包程序，通过cron运行，要使用logrotate打包你的rails日志，只需要在/etc/logrotate.d/目录下为你的rails应用创建一个配置文件即可，配置文件的语法如下： /path/to/your/app/log/production.log { &#160;&#160;compress &#160;&#160;weekly &#160;&#160;rotate 10 &#160;&#160;create 0600 user group &#160;&#160;missingok &#160;&#160;# Use bzip2 for compress. &#160;&#160;compresscmd /usr/bin/bzip2 &#160;&#160;uncompresscmd /usr/bin/bunzip2 &#160;&#160;compressoptions -9 &#160;&#160;compressext .bz2 &#160;&#160;postrotate &#160;&#160;&#160;&#160;cd /path/to/your/app &#038;&#038; touch tmp/restart.txt &#160;&#160;endscript } weekly表示每周打包一次，你也可以根据需要改为：daily, monthly, yearly，或者通过size来设置当日志超过某个尺寸后进行打包，rotate用于指定要保存的日志文件个数，最后的postrotate用于指定日志打包后需要执行的命令，如果你使用passenger，上面的例子告诉passenger重启rails应用以使用新的日志文件，更多的参数可以参看logrotate的帮助：http://linux.die.net/man/8/logrotate 在保存后最好通过下面的命令手动测试一下你的配置文件，以确保没有错误。 $ sudo /usr/sbin/logrotate /etc/logrotate.d/yourapp -v rotating pattern: /path/to/your/app/log/production.log weekly (10 rotations) empty log files are rotated, old logs [...]]]></description>
			<content:encoded><![CDATA[<p>logrotate是Linux系统的日志打包程序，通过cron运行，要使用logrotate打包你的rails日志，只需要在/etc/logrotate.d/目录下为你的rails应用创建一个配置文件即可，配置文件的语法如下：</p>
<p class="code"><code class="ruby"><br />
/path/to/your/app/log/production.log {<br />
&nbsp;&nbsp;compress<br />
&nbsp;&nbsp;weekly<br />
&nbsp;&nbsp;rotate 10<br />
&nbsp;&nbsp;create 0600 user group<br />
&nbsp;&nbsp;missingok<br />
&nbsp;&nbsp;# Use bzip2 for compress.<br />
&nbsp;&nbsp;compresscmd /usr/bin/bzip2<br />
&nbsp;&nbsp;uncompresscmd /usr/bin/bunzip2<br />
&nbsp;&nbsp;compressoptions -9<br />
&nbsp;&nbsp;compressext .bz2<br />
&nbsp;&nbsp;postrotate<br />
&nbsp;&nbsp;&nbsp;&nbsp;cd /path/to/your/app &#038;&#038; touch tmp/restart.txt<br />
&nbsp;&nbsp;endscript<br />
}</code></p>
<p>weekly表示每周打包一次，你也可以根据需要改为：daily, monthly, yearly，或者通过size来设置当日志超过某个尺寸后进行打包，rotate用于指定要保存的日志文件个数，最后的postrotate用于指定日志打包后需要执行的命令，如果你使用passenger，上面的例子告诉passenger重启rails应用以使用新的日志文件，更多的参数可以参看logrotate的帮助：<a href="http://linux.die.net/man/8/logrotate">http://linux.die.net/man/8/logrotate</a></p>
<p>在保存后最好通过下面的命令手动测试一下你的配置文件，以确保没有错误。</p>
<p class="code"><code><br />
$ sudo /usr/sbin/logrotate /etc/logrotate.d/yourapp -v<br />
rotating pattern: /path/to/your/app/log/production.log weekly (10 rotations)<br />
empty log files are rotated, old logs are removed<br />
considering log /path/to/your/app/log/production.log<br />
&nbsp;&nbsp;log does not need rotating<br />
not running postrotate script, since no logs were rotated<br />
</code></p>
<p>如果你的配置正确，会看到类似上面的输出，否则会看到错误提示。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/rotate-rails-log-with-logrotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>处理InvalidAuthenticityToken异常</title>
		<link>http://www.letrails.cn/archives/handle-invalidauthenticitytoken-exception/</link>
		<comments>http://www.letrails.cn/archives/handle-invalidauthenticitytoken-exception/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 00:56:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[技巧]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=211</guid>
		<description><![CDATA[Rails 2.3引入的防止跨站攻击的功能在某些情况下会导致用户正常使用时也产生InvalidAuthenticityToken异常，比如某个表单需要登录后才能提交，但是用户在登录后，过了很长一段时间才提交，此时Session已经失效，就会导致这个异常，有些人会直接忽略验证AuthenticityToken的filter，但是这样有安全隐患，比较好的处理方法就是在ApplicationController中增加一个全局Handler： class ApplicationController &#60; ActionController::Base &#38;nsp; rescue_from ActionController::InvalidAuthenticityToken, :with =&#62; :bad_token &#160;&#160; def bad_token &#160;&#160;&#160;&#160;flash[:notice] = "Your session has expired." &#160;&#160;&#160;&#160;respond_to do &#124;accepts&#124; &#160;&#160;&#160;&#160;&#160;&#160;accepts.html do &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;store_location &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redirect_to(:controller =&#62; '/sessions', :action =&#62; 'new') and return false &#160;&#160;&#160;&#160;&#160;&#160; end &#160;&#160;&#160;&#160;&#160;&#160; accepts.js do &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;store_location &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;render :update do &#124;page&#124; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;page.redirect_to(:controller =&#62; '/sessions', :action =&#62; 'new') and return false &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;end &#160;&#160;&#160;&#160;&#160;&#160;end &#160;&#160;&#160;&#160;end &#160;&#160;end [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.3引入的防止跨站攻击的功能在某些情况下会导致用户正常使用时也产生InvalidAuthenticityToken异常，比如某个表单需要登录后才能提交，但是用户在登录后，过了很长一段时间才提交，此时Session已经失效，就会导致这个异常，有些人会直接忽略验证AuthenticityToken的filter，但是这样有安全隐患，比较好的处理方法就是在ApplicationController中增加一个全局Handler：</p>
<p class="code"><code class="ruby"><br />
class ApplicationController &lt; ActionController::Base &amp;nsp; rescue_from ActionController::InvalidAuthenticityToken, :with =&gt; :bad_token<br />
&nbsp;&nbsp; def bad_token<br />
&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Your session has expired."<br />
&nbsp;&nbsp;&nbsp;&nbsp;respond_to do |accepts|<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accepts.html do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;store_location<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect_to(:controller =&gt; '/sessions', :action =&gt; 'new') and return false<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; accepts.js do<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;store_location<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :update do |page|<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.redirect_to(:controller =&gt; '/sessions', :action =&gt; 'new') and return false<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br />
&nbsp;&nbsp;&nbsp;&nbsp;end<br />
&nbsp;&nbsp;end<br />
end</code></p>
<p>来自：<a href="http://gist.github.com/94801">http://gist.github.com/94801</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/handle-invalidauthenticitytoken-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ar_mailer Rails 3 插件</title>
		<link>http://www.letrails.cn/archives/ar_mailer-rails-3-plugi/</link>
		<comments>http://www.letrails.cn/archives/ar_mailer-rails-3-plugi/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 09:52:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=205</guid>
		<description><![CDATA[ar_mailer是个很不错的邮件发送插件，工作原理是将邮件发送分为两部分，首先将邮件保存到数据库，然后在调用后台程序实现异步发送，可惜的是目前还不支持Rails 3，于是花了一个下午，将它移植到了Rails 3，这就是：ar_mailer_rails3。 安装 sudo gem install ar_mailer_rails 使用 script/rails generate ar_mailer_rails3 Email 你也可以将Email替换为其它名字，邮件将会被保存到这个Model中。 Generator会自动生成一个initializer： # config/initializers/ar_mailer.rb ActionMailer::Base.add_delivery_method :active_record, &#160;&#160;&#160;&#160;ArMailerRails3::ActiveRecord, :email_class => Email ActionMailer::Base.delivery_method = :active_record 如果你不希望所有Mailer都使用ActiveRecord发送，也可以在Mailer中单独定义： class MyMailer < ActionMailer::Base &#160;&#160; self.delivery_method = :active_record end 命令行的使用方式和ar_mailer是一样的，只不过为了和ar_mailer 2保持兼容，命令行的程序变成了ar_mailer_rails3: $ ar_sendmail_rails3 -h 详细的使用方法可参看这里]]></description>
			<content:encoded><![CDATA[<p><a href="http://seattlerb.org/ar_mailer">ar_mailer</a>是个很不错的邮件发送插件，工作原理是将邮件发送分为两部分，首先将邮件保存到数据库，然后在调用后台程序实现异步发送，可惜的是目前还不支持Rails 3，于是花了一个下午，将它移植到了Rails 3，这就是：<a href="http://github.com/yzhang/ar_mailer_rails3">ar_mailer_rails3</a>。</p>
<p>安装</p>
<p>sudo gem install ar_mailer_rails</p>
<p>使用</p>
<p>script/rails generate ar_mailer_rails3 Email</p>
<p>你也可以将Email替换为其它名字，邮件将会被保存到这个Model中。</p>
<p>Generator会自动生成一个initializer：</p>
<p class="code"><code class="ruby"><br />
# config/initializers/ar_mailer.rb<br />
ActionMailer::Base.add_delivery_method :active_record,<br />
&nbsp;&nbsp;&nbsp;&nbsp;ArMailerRails3::ActiveRecord, :email_class => Email<br />
ActionMailer::Base.delivery_method = :active_record<br />
</code></p>
<p>如果你不希望所有Mailer都使用ActiveRecord发送，也可以在Mailer中单独定义：</p>
<p class="code"><code class="ruby"><br />
class MyMailer < ActionMailer::Base<br />
&nbsp;&nbsp; self.delivery_method = :active_record<br />
end<br />
</code></p>
<p>命令行的使用方式和ar_mailer是一样的，只不过为了和ar_mailer 2保持兼容，命令行的程序变成了ar_mailer_rails3:</p>
<p>$ ar_sendmail_rails3 -h</p>
<p>详细的使用方法可<a href="http://github.com/adzap/ar_mailer">参看这里</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/ar_mailer-rails-3-plugi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>豆瓣API Rails 插件</title>
		<link>http://www.letrails.cn/archives/douban-api-rails-plugin/</link>
		<comments>http://www.letrails.cn/archives/douban-api-rails-plugin/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 08:41:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=199</guid>
		<description><![CDATA[目前只支持Rails 3 beta，地址：http://github.com/yzhang/douban 安装 $ gem install douban 使用 1. 修改Gemfile: gem 'douban', :require => 'oauth' require 'douban' 2. 运行generator $ script/rails generate douban 3. 修改config/douban.yml, 并在ApplicationController中包含DoubanHelper: class ApplicationController &#160;&#160;include DoubanHelper end 4. 然后在你的application layout中加入： &#60;% if douban_authorized? %&#62; &#160;&#160;&#60;%= link_to '注销豆瓣登录', douban_logout_path %&#62; &#60;% else %&#62; &#160;&#160;&#60;%= link_to "使用豆瓣帐号登录", douban_login_path %&#62; &#60;% end %&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>目前只支持Rails 3 beta，地址：<a href="http://github.com/yzhang/douban">http://github.com/yzhang/douban</a></p>
<p>安装</p>
<p>$ gem install douban</p>
<p>使用</p>
<p>1. 修改Gemfile:</p>
<p class="code"><code class="ruby"><br />
gem 'douban', :require => 'oauth'<br />
require 'douban'<br />
</code></p>
<p>2. 运行generator</p>
<p>$ script/rails generate douban</p>
<p>3. 修改config/douban.yml, 并在ApplicationController中包含DoubanHelper:</p>
<p class="code"><code class="ruby"><br />
class ApplicationController<br />
&nbsp;&nbsp;include DoubanHelper<br />
end<br />
</code></p>
<p>4. 然后在你的application layout中加入：</p>
<p class="code"><code class="ruby"><br />
&lt;% if douban_authorized? %&gt;<br />
&nbsp;&nbsp;&lt;%= link_to '注销豆瓣登录', douban_logout_path %&gt;<br />
&lt;% else %&gt;<br />
&nbsp;&nbsp;&lt;%= link_to "使用豆瓣帐号登录", douban_login_path %&gt;<br />
&lt;% end %&gt;<br />
</code></p>
<p>还可以使用下面的Helper:</p>
<ul>
<li>douban_auth_or_login_required, 方便和RESTFUL_AUTH一起使用</li>
<li>douban_auth_required, 可以作为before_filter使用</li>
</ul>
<p>5. 要访问用户资源，可以调用：</p>
<p class="code"><code class="ruby"><br />
douban.get('/people/yzhang')<br />
douban.post('/reviews', review)<br />
</code></p>
<p>6. 要验证用户是否已授权，可以调用:</p>
<p class="code"><code class="ruby"><br />
douban.authorized?<br />
=> true  # 如果已经获得access token<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/douban-api-rails-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 3 beta 中文邮件乱码问题解决</title>
		<link>http://www.letrails.cn/archives/rails-3-beta-multibyte-email-garbled/</link>
		<comments>http://www.letrails.cn/archives/rails-3-beta-multibyte-email-garbled/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 03:24:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[其它]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=195</guid>
		<description><![CDATA[更新： Mail 2.2 已解决此问题 如果你升级到了Rails 3 Beta，并且使用了ActionMailer，那么你会发现邮件标题出现有规律的乱码，每个8的倍数的地方都出现了奇怪的字符，这是ActionMailer和Mail gem配合出现了问题，解决的办法很简单，修改ActionMailer::Base#quote_fields，将这个函数改成如下： def quote_fields!(headers, charset) #:nodoc: &#160;&#160;m = @_message &#160;&#160;m.subject &#124;&#124;= headers[:subject] if headers[:subject] &#160;&#160;m.to &#124;&#124;= headers[:to] if headers[:to] &#160;&#160;m.from &#124;&#124;= headers[:from] if headers[:from] &#160;&#160;m.cc &#124;&#124;= headers[:cc] if headers[:cc] &#160;&#160;m.bcc &#124;&#124;= headers[:bcc] if headers[:bcc] &#160;&#160;m.reply_to &#124;&#124;= headers[:reply_to] if headers[:reply_to] end 给Rails Core提了一个Patch，但是因为考虑不周，没有通过，所以官方的解决方案恐怕要等到Mail 2.2或者Rails 3 Beta1。]]></description>
			<content:encoded><![CDATA[<p><strong>更新： Mail 2.2 已解决此问题</strong></p>
<p>如果你升级到了Rails 3 Beta，并且使用了ActionMailer，那么你会发现邮件标题出现有规律的乱码，每个8的倍数的地方都出现了奇怪的字符，这是ActionMailer和Mail gem配合出现了问题，解决的办法很简单，修改ActionMailer::Base#quote_fields，将这个函数改成如下：</p>
<p class="code"><code class="ruby"><br />
def quote_fields!(headers, charset) #:nodoc:<br />
&nbsp;&nbsp;m = @_message<br />
&nbsp;&nbsp;m.subject  ||= headers[:subject]  if headers[:subject]<br />
&nbsp;&nbsp;m.to       ||= headers[:to]       if headers[:to]<br />
&nbsp;&nbsp;m.from     ||= headers[:from]     if headers[:from]<br />
&nbsp;&nbsp;m.cc       ||= headers[:cc]       if headers[:cc]<br />
&nbsp;&nbsp;m.bcc      ||= headers[:bcc]      if headers[:bcc]<br />
&nbsp;&nbsp;m.reply_to ||= headers[:reply_to] if headers[:reply_to]<br />
end<br />
</code></p>
<p>给Rails Core<a href="https://rails.lighthouseapp.com/projects/8994/tickets/4294-patch-remove-quote_fields-from-actionmailer">提了一个Patch</a>，但是因为考虑不周，没有通过，所以官方的解决方案恐怕要等到Mail 2.2或者Rails 3 Beta1。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/rails-3-beta-multibyte-email-garbled/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>升级到Rails 3 Beta</title>
		<link>http://www.letrails.cn/archives/upgrade-to-rails-3-beta/</link>
		<comments>http://www.letrails.cn/archives/upgrade-to-rails-3-beta/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 03:54:35 +0000</pubDate>
		<dc:creator>yuanyi</dc:creator>
				<category><![CDATA[指南]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://www.letrails.cn/?p=184</guid>
		<description><![CDATA[安装 gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n gem install rails --pre 这里会遇到一个rack-mount依赖问题，可以通过手动安装0.4.0版的rack-mount解决： sudo gem install rack-mount -v=0.4.0 升级应用 rails_upgrade(http://github.com/rails/rails_upgrade)是Rails官方的Rails3升级辅助插件，它提供了几个Rake任务帮助你轻松完成升级。 1. 执行 rails:upgrade:backup 备份会被Rails 3覆盖的文件，比如：application_controller.rb会被重命名为application_controller.rb.rails2 2. 执行 rake rails:upgrade:routes 生成Rails 3格式的routes.rb，以及rake rails:upgrade:gems生成新的Gemfile 3. 在你的程序目录执行: rails . 注意不要覆盖routes.rb和Gemfile 接下来要做的就是将备份的rails2文件合并到新生成的文件中，再次启动应用，如果幸运的话，你的应用现在已经运行在Rails 3 beta上了，注意检查Log中的Deprecated Warning，并将其替换为Rails 3的方式，关于Rails 3的新特性请参看Release Notes：http://guides.rails.info/3_0_release_notes.html ActionView Rails 3重写了View Helper，并作废了以前的link_to_function, [...]]]></description>
			<content:encoded><![CDATA[<p>安装</p>
<p class="code"><code class="ruby"><br />
gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n<br />
gem install rails --pre<br />
</code></p>
<p>这里会遇到一个rack-mount依赖问题，可以通过手动安装0.4.0版的rack-mount解决：</p>
<p>sudo gem install rack-mount -v=0.4.0</p>
<p>升级应用</p>
<p><a href="http://github.com/rails/rails_upgrade">rails_upgrade(http://github.com/rails/rails_upgrade)</a>是Rails官方的Rails3升级辅助插件，它提供了几个Rake任务帮助你轻松完成升级。</p>
<p>1. 执行 rails:upgrade:backup 备份会被Rails 3覆盖的文件，比如：application_controller.rb会被重命名为application_controller.rb.rails2<br />
2. 执行 rake rails:upgrade:routes 生成Rails 3格式的routes.rb，以及rake rails:upgrade:gems生成新的Gemfile<br />
3. 在你的程序目录执行: rails .  注意不要覆盖routes.rb和Gemfile</p>
<p>接下来要做的就是将备份的rails2文件合并到新生成的文件中，再次启动应用，如果幸运的话，你的应用现在已经运行在Rails 3 beta上了，注意检查Log中的Deprecated Warning，并将其替换为Rails 3的方式，关于Rails 3的新特性请参看Release Notes：<a href="http://guides.rails.info/3_0_release_notes.html">http://guides.rails.info/3_0_release_notes.html</a></p>
<p>ActionView</p>
<p>Rails 3重写了View Helper，并作废了以前的link_to_function, link_to_remote, remote_form_for等AJAX Helper，而改为为link_to, form_for增加:remote => true来实现。</p>
<p>link_to_function(text, function) => link_to(text, &#8216;#&#8217;, function)<br />
link_to_remote(text, {:url => url}) => link_to(text, url, :remote => true)<br />
remote_form_for(object) => form_for(object, :remote => true)</p>
<p>Passenger</p>
<p>Passenger从2.2.9版本开始支持Rails 3，如果你的版本小于2.2.9，请先升级，另外需要注意的是，Rails 3会在你的应用目录下创建一个config.ru文件，如果需要以development模式运行你的应用，需要做点改变：</p>
<p>1. 在你的apache配置中加入： RackEnv development<br />
2. 删除config.ru，这样passenger会继续使用RailsEnv</p>
<p>jQuery</p>
<p>Rails 3默认是使用Prototype的，如果要使用jQuery，<a href="http://github.com/rails/jquery-ujs.git">jquery-ujs（http://github.com/rails/jquery-ujs.git）</a>项目提供了jQuery版本的rails.js，下载并替换默认的rails.js，然后在 application.html.erb的head部分增加:</p>
<p class="code"><code class="ruby"><br />
&lt;meta name="csrf-token" content="&lt;%= form_authenticity_token %&gt;" /&gt;<br />
&lt;meta name="csrf-param" content="authenticity_token" /&gt;<br />
</code></p>
<p>Paperclip</p>
<p>Paperclip目前还不支持Rails 3，不过Github上已经有了一个Rails 3的分支，我们可以直接使用这个分支：</p>
<p class="code"><code class="ruby"><br />
git submodule add -b rails3 git://github.com/thoughtbot/paperclip.git vendor/plugins/paperclip<br />
git submodule init<br />
</code></p>
<p>但是这个分支目前工作也不正常，还需要打一个补丁：</p>
<p class="code"><code class="ruby"><br />
# in lib/paperclip/attachment.rb at line 293<br />
def callback which #:nodoc:<br />
&nbsp;&nbsp;# replace this line...<br />
&nbsp;&nbsp;# instance.run_callbacks(which, @queued_for_write){|result,obj| result == false }<br />
&nbsp;&nbsp;# with this:<br />
&nbsp;&nbsp;instance.run_callbacks(which, @queued_for_write)<br />
end<br />
</code></p>
<p>此处参考：<a href="http://jameswilding.net/2010/02/07/paperclip-on-rails-3-beta/">http://jameswilding.net/2010/02/07/paperclip-on-rails-3-beta/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.letrails.cn/archives/upgrade-to-rails-3-beta/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
