有没有办法Rails 3.0.x可以默认使用Thin?

我为我的开发/测试环境中的每个应用程序运行Thin webserver。 当我使用Mongrel和Rails 2.x时,我只需要输入script/server来运行我选择的网络服务器。 但是使用Rails 3,我必须每次都指定Thin。 有没有办法让我的Rails应用程序只需输入rails s而不是rails s thin就可以运行Thin?

从Rails 3.2rc2开始,当gem’thin gem 'thin'在你的Gemfile中时,现在默认运行thin来调用rails server ! 感谢这个拉取请求: https : //github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7

对我很有用。

是的,这是可能的。

rails s命令在一天结束时的工作方式是掉到Rack并让它选择服务器。 默认情况下,Rack处理程序将尝试使用mongrel ,如果找不到mongrel,它将与webrick一起使用。 我们所要做的就是稍微修补处理程序。 我们需要将我们的补丁插入rails脚本本身。 这是你做的,破解你的script/rails文件。 默认情况下,它应如下所示:

 #!/usr/bin/env ruby # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. APP_PATH = File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/boot', __FILE__) require 'rails/commands' 

我们在require 'rails/commands'行之前插入我们的补丁。 我们的新文件应如下所示:

 #!/usr/bin/env ruby # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. APP_PATH = File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/boot', __FILE__) require 'rack/handler' Rack::Handler.class_eval do def self.default(options = {}) # Guess. if ENV.include?("PHP_FCGI_CHILDREN") # We already speak FastCGI options.delete :File options.delete :Port Rack::Handler::FastCGI elsif ENV.include?("REQUEST_METHOD") Rack::Handler::CGI else begin Rack::Handler::Mongrel rescue LoadError begin Rack::Handler::Thin rescue LoadError Rack::Handler::WEBrick end end end end end require 'rails/commands' 

请注意,它现在将尝试使用Mongrel,如果出现错误,请尝试使用Thin,然后再使用Webrick。 现在当你输入rails s我们得到了我们所追求的行为。

script/rails ,以下工作原理:

 APP_PATH = File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/boot', __FILE__) require 'rack/handler' Rack::Handler::WEBrick = Rack::Handler::Thin require 'rails/commands' 

只需将thin,cd安装到您的应用所在的目录并运行thin start。 在这里完美运作。 🙂

您可以使用http://www.softiesonrails.com/2008/4/27/using-thin-instead-of-mongrel根据需要进行更改。 (它是我用过的那个)