如何在生产模式下使用rails 3.1建立一个完整的"hello world"站点?

use*_*307 2 production ruby-on-rails mode

我正在努力在生产模式下设置基于rails的网站.中间目标是建立一个非常简单的网站.我使用的是Rails 3.1.0,rake 0.9.2.2和Ruby 1.9.2.

这是我尝试过的,到目前为止结果不成功:

最初,确保未设置RAILS_ENV

rails new test_project

cd test_project

rails generate scaffold User name:string email:string

rake db:migrate

rails s
Run Code Online (Sandbox Code Playgroud)

浏览到localhost:3000或localhost:3000/users

事情看起来不错.

现在,尝试将其设置为生产:

export RAILS_ENV=production

rake db:migrate

rake assets:precompile

rails s
Run Code Online (Sandbox Code Playgroud)

浏览到localhost:3000

问题: Routing Error; No route matches [GET] "/"

杀死铁轨

在config/routes中,添加 root :to => 'users#index'

rails s
Run Code Online (Sandbox Code Playgroud)

现在可以浏览到localhost:3000和localhost:3000/users

但是,rails会产生以下错误:

Started GET "/assets/application-00960e5186894b532975562d59176a6a.css" for 127.0.0.1 at 2011-11-26 23:09:44 -0800

  ActionController::RoutingError (No route matches [GET] "/assets/application-00960e5186894b532975562d59176a6a.css"):

  Started GET "/assets/application-ae30e133eabbb10d9464189d3fb71e25.js" for 127.0.0.1 at 2011-11-26 23:09:44 -0800

  ActionController::RoutingError (No route matches [GET] "/assets/application-ae30e133eabbb10d9464189d3fb71e25.js"):
Run Code Online (Sandbox Code Playgroud)

任何人都可以阐明如何解决上述简单的尝试,使Rails 3.1项目在"生产"模式下工作?

Joh*_*ter 5

基本问题是,在"生产"模式下运行应用程序的默认配置会对部署做出一些假设 - 主要是您正在使用另一个Web服务器(例如,nginx,apache)来提供静态资产.您正在获取文件未找到错误,因为默认情况下Rails不会在生产模式下提供静态资产

如果您尝试使用WEBrick复制"生产"环境,则需要将其配置为提供静态资产.您只需在production.rb中翻转布尔值即可

环境/ production.rb#禁用Rails的静态资产服务器(Apache或nginx已经这样做了)config.serve_static_assets = false

一旦你进行了更改并重新启动服务器,你将使用WEBrick为你预编译的资产提供服务,虽然效率低下,但肯定会让你知道它在生产中的含义.