0

我正在使用 postgresql 在 debian 服务器上部署我的第一个 railsapp。

Rails 3.2.9 Ruby 1.9.3-p327 pg 0.14.1

部署后,当我做一个

bundle exec rake db:reset

它失败并出现以下错误:

/var/www/opf/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208: [BUG] Segmentation fault

以及一长串错误消息。

虽然我的 database.yml 在我的开发工作中正常工作:

我找不到 postgresql_adapter 导致错误的原因。

- - 更新

如果我输入错误的数据库名、用户名和密码,结果是一样的。我用 psql my_db my_user 测试了数据库连接,它可以工作。如果我做一个

bundle exec rake db:reset RAILS_ENV=production

我收到一个错误:

undefined method `sass' for #<Rails::Application::Configuration:0x00000000dea738>

虽然sasssass-railshaml-rails gem 存在于我的 /var/www/opf/shared/bundle/ruby/1.9.1/gems

====== 更新 =======

这是我的 database.yml :

development:
  host: localhost
  port: 5432
  adapter: postgresql
  encoding: utf8
  database: mydevdb
  pool: 5
  username: myuser
  password: mypass

test:
  host: localhost
  port: 5432
  adapter: postgresql
  encoding: utf8
  database: mytestdb
  pool: 5
  username: myuser
  password: mypass

production:
  host: localhost
  port: 5432
  adapter: postgresql
  encoding: utf8
  database: myproddb
  pool: 5
  username: myuser
  password: mypass

这是我的 Gemfile :

source 'https://rubygems.org'

gem 'rails', '~>3.2'
# Ressource : https://github.com/ged/ruby-pg
gem 'pg', '>= 0.14'
# https://github.com/indirect/haml-rails
gem 'haml-rails', '~> 0.3'

group :developpement do
  # Ressource : https://github.com/rspec/rspec-rails
  gem 'rspec-rails', '>= 2.11'
  # Ressource : https://rvm.io//integration/capistrano/
  gem 'capistrano', '>= 2.12'
  # Ressource : https://github.com/btelles/faker
  gem 'faker', '>= 1.0'
  # Ressource : https://rvm.io//integration/capistrano/
  gem 'rvm-capistrano'
end

group :test do 
  gem 'rspec', '>= 2.11'
  gem 'webrat', '>= 0.7'
  gem 'spork-rails', '>= 3.2'
  gem 'factory_girl_rails', '>= 4.0'
end

group :assets do
  # Faciliter la rédaction de feuilles de style CSS
  # Ressource : http://sass-lang.com/
  gem 'sass-rails',   '>= 3.2.3'
  gem 'coffee-rails', '>= 3.2.1'
  # Ressource : http://compass-style.org/
  gem 'compass-rails', '>= 1.0'

  # See https://github.com/sstephenson/execjs
  #readme for more supported runtimesa
  # execjs and therubyracer are ncessary for compass-rails gem to work properly
  gem 'execjs'
  gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
# Ressource : https://github.com/ctran/annotate_models
gem 'annotate'
# Ressource : https://github.com/asanghi/excel_rails
gem 'excel_rails', '~> 0.3'
# Ressource : https://github.com/voraz/spreadsheet
gem 'spreadsheet', '~> 0.7'
# Ressource : https://github.com/lomba/schema_plus
gem 'schema_plus', '~> 0.4'
# Resource : https://github.com/tchandy/octopus
gem 'ar-octopus', '~> 0.3'
# Resource : https://github.com/ernie/squeel
gem 'squeel', '~> 1.0'
# Resource : https://github.com/plataformatec/devise
gem 'devise', '~> 2.1'
# Resource : https://github.com/martinrehfeld/role_model
gem 'role_model'
# Resource : https://github.com/stffn/declarative_authorization
gem 'declarative_authorization'
# Resource : https://github.com/francesc/rails-translate-routes
gem 'rails-translate-routes', '~> 0.1'

我做了更多的测试,并意识到问题来自初始化过程。在某些时候,我的应用程序的初始化会使 Ruby 解释器崩溃。我删除了我的 config/application.rb 的一些行。结果至少将崩溃变成了关于“未初始化的常量设计”的错误。我用谷歌搜索,发现我在 application.rb 中添加了一个“需要'设计'”。所以我做了,但错误和 Ruby 崩溃又来了。

这个问题似乎与资产管道和初始化有关。我注意到以下行使我的初始化使 Ruby 解释器崩溃,因此我对其进行了评论:

Bundler.require(*Rails.groups(:assets => %w(development test)))

我的 config/application.rb :

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Without this I got a "unitialized Constant Devise" error
require 'devise'

# THIS ALL STUFF MAKES CRASH RAILS INITIALIZATION, so I commented it.
if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  #Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Opf
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # Pour utiliser la traduction française des messages d'erreur standards
    config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = 'fr'

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Enable escaping HTML in JSON.
    config.active_support.escape_html_entities_in_json = true

    # Use SQL instead of Active Record's schema dumper when creating the database.
    # This is necessary if your schema can't be completely dumped by the schema dumper,
    # like if you have constraints or database-specific column types
    config.active_record.schema_format = :sql

    # Enforce whitelist mode for mass assignment.
    # This will create an empty whitelist of attributes available for mass-assignment for all models
    # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
    # parameters by using an attr_accessible or attr_protected declaration.
    config.active_record.whitelist_attributes = true

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

    # =======  DEBUG : without this, Capistrano Assets:precompile crash ========
    config.assets.initialize_on_precompile = false
    # ==============================================================

   # Make crashing Initialization, so I commented
#    config.generators do |g|
#      g.test_framework :rspec
#      g.fixture_replacement :factory_girl
#      g.stylesheets false
      # cette option ne doit pas être activée car elle est géré (et activée) par la gem 'haml-rails'
#      # g.template-engine :haml
#    end

#    config.sass.preferred_syntax = :sass

    # To replace 'div' by 'span' within error-feedbacks
    config.action_view.field_error_proc = Proc.new do |html_tag, instance|
      # Ressources : http://stackoverflow.com/questions/5267998/rails-3-field-with-errors-wrapper-changes-the-page-appearance-how-to-avoid-t
      # Ressources : Railcast : http://railscasts.com/episodes/39-customize-field-error?autoplay=true
      class_attr_index = html_tag.index('class="')
      if class_attr_index
        html_tag.insert class_attr_index+7, 'field_with_errors '
      else
        html_tag.insert html_tag.index('>'), ' class="field_with_errors"'
      end
    end
  end
end

myapp 的成功测试总结:

  • Postgresql 数据库用户访问和等于 database.yml = WORK
  • @global gemset 中的宝石存在 = CHECK
  • 直接在服务器上从头开始创建一个“测试”应用程序 = WORK
4

1 回答 1

0

好的,经过一些非常长时间的研究和测试,我找出了问题的原因。

有几个错误:

  • 为了 execjs gem 正常工作,

    gem 'therubyracer', :platforms => :ruby

必须在生产组或 Gemfile 中的任何组之外

  • 为了对嵌套的 SASS-CSS 资产进行预编译rake assets:precompile( ),并且我必须添加以下行

    @import screen.css.sass

在我的 app/assets/stylesheets/application.css.sass 文件中。

  • 为了使捆绑器正常工作,我在“gem install bundler”期间收到一条消息,$PATH:要求在服务器端添加我的 /etc/profile 和 /root/.bashrc 文件。

  • 为了 capistrano deploy:update 任务正常工作,不要忘记添加require 'rvm/capistrano'config/deploy.rb 文件,以便 capistrano 可以找到(并执行)bundle命令。

但是通过所有这些修复,我仍然遇到与 postgresql 相同的错误:

/var/www/opf/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208: [BUG] Segmentation fault

因为我正在使用这个特殊配方以避免将 database.yml 存储在 github 上。并使用此解决方案命令 capistrano 找到我的食谱文件。我为这个食谱构建了一个 database.yml 模板,但不知何故我意识到问题出在我的模板上。

我必须删除每个数据库上的这两行,以便 rails 初始化正常工作:

  host: localhost
  port: 5432

也许生成的 database.yml 已损坏,我没有测试重新添加这些(更正)两行。我已经在这上面花了几个星期...

--- 一般建议 ---

现在,我向那些迫切希望找到解决问题的开发人员(就像我一样)提供一些建议:在大量寻找解决方案之后,停止它。

只需使用rails new newapp --database=postgresqlrails new -h用于此命令的数据库参数的帮助)构建一个新的非常基本的 rails 应用程序,然后使用rails generate scaffold mymodel mycolumn.

git init在您的应用程序目录中,使用和 Capistrano初始化 Gitcapify . 然后只需为您的数据库添加 gem(例如 postgresql 的 pg),运行捆绑安装。然后用你自己的基本 database.yml 将它连接到你的数据库,让你测试它是否可以在你的开发工作站上运行。提交并推送到您的 Git 存储库,然后使用 capistrano 进行部署并检查它是否在服务器端运行。

然后逐步将(宝石、迁移和您自己的个人库)添加到您的新应用程序中,直到它不起作用。

同时,将 newapp 的工作位添加到服务器端失败的应用程序中,直到它工作为止(不要在开发工作站端的应用程序上更改任何内容)。

这迟早会引导你找到解决方案。

于 2013-03-27T10:44:48.017 回答