神奇的.gems

在heroku 中托管项目如何才能把自己所需要的gem包install了呢?
例如:在我们本机开发时需要用到 coderay和rake-codehighlighter这两个gem.那么直接使用 gem install coderay 和 gem install rack-codehighlighter 就行了.然后再在项目中使用

1    require 'coderay'
2    require 'rack/codehighlighter'

将这两个包加载到程序中就行了.可是部署到heroku中却出现问题:`gemoriginalrequire': no such file to load -- coderay (LoadError),如下:

 1    2011-04-30T02:13:37+00:00 app[web.1]: /usr/ruby1.8.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- coderay (LoadError)
 2    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
 3    2011-04-30T02:13:37+00:00 app[web.1]:   from config.ru:5
 4    2011-04-30T02:13:37+00:00 app[web.1]:   from /home/heroku_rack/heroku.ru:23
 5    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/builder.rb:29:in `instance_eval'
 6    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/builder.rb:29:in `initialize'
 7    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/builder.rb:46:in `new'
 8    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/builder.rb:46:in `map'
 9    2011-04-30T02:13:37+00:00 app[web.1]:   from /home/heroku_rack/heroku.ru:18
10    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/builder.rb:29:in `instance_eval'
11    2011-04-30T02:13:37+00:00 app[web.1]:   from /usr/ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/builder.rb:29:in `initialize'
12    2011-04-30T02:13:37+00:00 app[web.1]:   from /home/heroku_rack/heroku.ru:11:in `new'

原来在本地安装了并不一定在heroku中也安装.google了一下,发现了一个神奇的文件 : .gems

.gems文件可以告诉heroku你要安装那些文件.具体实现方法:

在项目的根目录下应该有一个隐藏文件 .gems文件,没有的话新建一个.然后在里边输入需要install的gem包.我的如下:

1builder
2rdiscount
3toto
4coderay
5rack-codehighlighter

当然后边还有一些其他选项,具体可以参考这里:http://devcenter.heroku.com/articles/gemmanifest

然后用git命令将其push到heroku上.

1git add .gems
2git commit -m "add gems"
3git push heroku master

运行结果如下:

 1Counting objects: 8, done.
 2Delta compression using up to 2 threads.
 3    Compressing objects: 100% (6/6), done.
 4    Writing objects: 100% (6/6), 563 bytes, done.
 5    Total 6 (delta 2), reused 0 (delta 0)
 6
 7    -----> Heroku receiving push
 8    -----> Rack app detected
 9
10    -----> Installing gem builder from http://rubygems.org
11               Successfully installed builder-3.0.0
12               1 gem installed
13
14    -----> Installing gem rdiscount from http://rubygems.org
15               Building native extensions.  This could take a while...
16               Successfully installed rdiscount-1.6.8
17               1 gem installed
18
19    -----> Installing gem toto from http://rubygems.org
20               Successfully installed toto-0.4.9
21               1 gem installed
22
23    -----> Installing gem coderay from http://rubygems.org
24               Successfully installed coderay-0.9.7
25               1 gem installed
26
27    -----> Installing gem rack-codehighlighter from http://rubygems.org
28               Building native extensions.  This could take a while...
29               Successfully installed nokogiri-1.4.4
30               Successfully installed rack-codehighlighter-0.4.7
31               2 gems installed
32
33               Compiled slug size is 2.5MB
34    -----> Launching... done
35               http://shiyj.heroku.com deployed to Heroku
36
37    To git@heroku.com:shiyj.git
38         f8fa0b1..a1f41c9  master -> master

再次刷新,程序可以运行了.

Powered by Engin & toto

comments powered by Disqus