回形针无法在 Apache/Passenger 下工作
我已将 Rails 应用程序部署在运行 Apache 和 Phusion Passenger 的临时/生产计算机上,但无法正确加载 Paperclip gem(或者至少无法让使用它的应用程序部分正常工作)。其他 gem 似乎加载得很好,当我在 RAILS_ROOT/config/environments/staging.rb 中将 config.cache_classes 设置为 false 时,应用程序的其余部分工作正常。如果将其设置为 true,我会在浏览器(和 apache 错误日志)中收到以下 Passenger 消息,并且根本无法使用该应用程序:
Error message:
undefined method `validates_attachment_presence' for #<Class:0xb618e938>
Exception class:
NoMethodError
我已经在 google 上搜索了相当长的一段时间,但无法找到我遇到的这个问题的解决方案。我希望我没有忽略一些明显的事情...
更新21/8:在同一个盒子上使用 sudo script/server -b 17x.7x.16x.3x -e staging 运行rails应用程序工作正常。这可能是权限问题并且 Passenger 无法找到已安装的 Paperclip Gem 吗?
希望您能给我一些指示,如果您需要更多信息,请告诉我!谢谢马克
关于我的环境的一些信息:
$ rails -v
Rails 2.3.5
RAILS_ROOT/config/environment.rb 有这样的设置:
config.gem "paperclip", :version => "~> 2.3"
Gems 环境:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /var/lib/gems/1.8/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /var/lib/gems/1.8
- /home/mark/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
服务器上的 Gems 列表:
$ gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.9, 2.3.5)
actionpack (3.0.9, 2.3.5)
activemodel (3.0.9)
activerecord (3.0.9, 2.3.5)
activeresource (3.0.9, 2.3.5)
activesupport (3.0.9, 2.3.5)
arel (2.1.3, 2.0.10)
builder (3.0.0, 2.1.2)
capistrano (2.6.0)
cocaine (0.1.0)
daemon_controller (0.2.6)
erubis (2.6.6)
fastthread (1.0.7)
highline (1.6.2)
i18n (0.4.2)
mail (2.2.19)
mime-types (1.16)
mysql (2.8.1)
net-scp (1.0.4)
net-sftp (2.0.5)
net-ssh (2.1.4)
net-ssh-gateway (1.1.0)
paperclip (2.3.12)
passenger (3.0.7)
polyglot (0.3.1)
rack (1.2.3, 1.1.0, 1.0.1)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (2.3.5)
rake (0.9.2, 0.8.7)
treetop (1.4.9)
tzinfo (0.3.29)
will_paginate (2.3.15)
Paperclip gem 安装在此处:
$ sudo find / -name 'paperclip'
/var/lib/gems/1.8/doc/paperclip-2.3.12/rdoc/files/lib/paperclip
/var/lib/gems/1.8/doc/paperclip-2.3.12/rdoc/files/lib/generators/paperclip
/var/lib/gems/1.8/gems/paperclip-2.3.12/lib/paperclip
/var/lib/gems/1.8/gems/paperclip-2.3.12/lib/generators/paperclip
/var/lib/gems/1.8/gems/paperclip-2.3.12/generators/paperclip
Apache 虚拟主机配置:
$ more /etc/apache2/sites-available/staging.mydomain.com
<VirtualHost *:80>
ServerName staging.mydomain.com
DocumentRoot /u/apps/mydomain.com/current/public
RailsEnv staging
</VirtualHost>
来自乘客页面的一些附加堆栈跟踪(顺便说一句:尝试过删除 will_paginate 插件,但结果完全相同;它在 active_record/base.rb 处抛出错误):
Backtrace:
# File Line Location
0 /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb 1959 in `method_missing_without_paginate'
1 /u/apps/mydomain.com/releases/20110705192718/vendor/plugins/will_paginate/lib/will_paginate/finder.rb 170 in `method_missing'
2 /u/apps/mydomain.com/releases/20110705192718/app/models/image.rb 5
3 /usr/lib/ruby/1.8/rubygems/custom_require.rb 31 in `gem_original_require'
4 /usr/lib/ruby/1.8/rubygems/custom_require.rb 31 in `require'
5 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 158 in `require'
6 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 265 in `require_or_load'
7 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 224 in `depend_on'
8 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 136 in `require_dependency'
9 /var/lib/gems/1.8/gems/rails-2.3.5/lib/initializer.rb 414 in `load_application_classes'
10 /var/lib/gems/1.8/gems/rails-2.3.5/lib/initializer.rb 413 in `each'
图像类的代码:
class Image < ActiveRecord::Base
belongs_to :user
validate_attachment_presence :photo
validates_uniqueness_of :photo_file_name, :scope => :user_id
has_attached_file :photo, :url => "/assets/:user/app/:class/:style_:basename.:
extension",
:path => "#{APP_PATH}/:user/app/:class/:style_:basename.:extension",
:styles => {
:thumb=> "50x50#",
:large => "400x" }
end
config/environment.rb
# Be sure to restart your server when you modify this file
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
config.time_zone = 'UTC'
config.active_record.observers = :user_observer #, :website_observer
config.gem "paperclip", :version => "~> 2.3"
end
SITE_NAME = 'Mydomain.com'
CMS_PREFIX = 'joomla'
配置/环境.rb/staging.rb
# Settings specified here will take precedence over those in config/environment.rb
# The staging environment is meant for finished, apps and final acceptance testing.
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_view.cache_template_loading = true
ActionController::Base.asset_host = "staging.mydomain.com"
DOCUMENT_ROOT = "/var/www"
APP_PATH = "/u/apps"
DB_PREFIX = "a001"
DB_USER = "joomla"
DB_PWD = "*****"
GIT_URL = "[email protected]:****/mydomain.git"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => :plain,
:user_name => "****@gmail.com",
:password => "****"
}
I've deployed my rails application on a staging/production machine running Apache and Phusion Passenger but cannot get the Paperclip gem to load correctly (or at least to get the piece of the application working that uses it). Other gems seem to load fine and when I set config.cache_classes
to false
in the RAILS_ROOT/config/environments/staging.rb the rest of the application works fine. If it is set to true I get the following Passenger message in the browser (and apache error log) and cannot use the application at all:
Error message:
undefined method `validates_attachment_presence' for #<Class:0xb618e938>
Exception class:
NoMethodError
I've googled around for quite some time but cannot find a solution for this issue I am having. I hope I am not overlooking something obvious...
UPDATE 21/8: On the same box running the rails app with sudo script/server -b 17x.7x.16x.3x -e staging works fine. Could this be a permission problem and Passenger not being able to find the installed Paperclip Gem?
Hope you can give me some pointers, let me know if you need more info! Thanks Mark
Some information on my environment:
$ rails -v
Rails 2.3.5
RAILS_ROOT/config/environment.rb has this set:
config.gem "paperclip", :version => "~> 2.3"
Gems environment:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /var/lib/gems/1.8/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /var/lib/gems/1.8
- /home/mark/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
Gems list on the server:
$ gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.9, 2.3.5)
actionpack (3.0.9, 2.3.5)
activemodel (3.0.9)
activerecord (3.0.9, 2.3.5)
activeresource (3.0.9, 2.3.5)
activesupport (3.0.9, 2.3.5)
arel (2.1.3, 2.0.10)
builder (3.0.0, 2.1.2)
capistrano (2.6.0)
cocaine (0.1.0)
daemon_controller (0.2.6)
erubis (2.6.6)
fastthread (1.0.7)
highline (1.6.2)
i18n (0.4.2)
mail (2.2.19)
mime-types (1.16)
mysql (2.8.1)
net-scp (1.0.4)
net-sftp (2.0.5)
net-ssh (2.1.4)
net-ssh-gateway (1.1.0)
paperclip (2.3.12)
passenger (3.0.7)
polyglot (0.3.1)
rack (1.2.3, 1.1.0, 1.0.1)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (2.3.5)
rake (0.9.2, 0.8.7)
treetop (1.4.9)
tzinfo (0.3.29)
will_paginate (2.3.15)
Paperclip gem is installed here:
$ sudo find / -name 'paperclip'
/var/lib/gems/1.8/doc/paperclip-2.3.12/rdoc/files/lib/paperclip
/var/lib/gems/1.8/doc/paperclip-2.3.12/rdoc/files/lib/generators/paperclip
/var/lib/gems/1.8/gems/paperclip-2.3.12/lib/paperclip
/var/lib/gems/1.8/gems/paperclip-2.3.12/lib/generators/paperclip
/var/lib/gems/1.8/gems/paperclip-2.3.12/generators/paperclip
Apache virtualhost config:
$ more /etc/apache2/sites-available/staging.mydomain.com
<VirtualHost *:80>
ServerName staging.mydomain.com
DocumentRoot /u/apps/mydomain.com/current/public
RailsEnv staging
</VirtualHost>
Some additional stack trace from the passenger page (btw: tried removing the will_paginate plugin, but the result is exactly the same; it throws an error at active_record/base.rb):
Backtrace:
# File Line Location
0 /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb 1959 in `method_missing_without_paginate'
1 /u/apps/mydomain.com/releases/20110705192718/vendor/plugins/will_paginate/lib/will_paginate/finder.rb 170 in `method_missing'
2 /u/apps/mydomain.com/releases/20110705192718/app/models/image.rb 5
3 /usr/lib/ruby/1.8/rubygems/custom_require.rb 31 in `gem_original_require'
4 /usr/lib/ruby/1.8/rubygems/custom_require.rb 31 in `require'
5 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 158 in `require'
6 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 265 in `require_or_load'
7 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 224 in `depend_on'
8 /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb 136 in `require_dependency'
9 /var/lib/gems/1.8/gems/rails-2.3.5/lib/initializer.rb 414 in `load_application_classes'
10 /var/lib/gems/1.8/gems/rails-2.3.5/lib/initializer.rb 413 in `each'
Code for the image class:
class Image < ActiveRecord::Base
belongs_to :user
validate_attachment_presence :photo
validates_uniqueness_of :photo_file_name, :scope => :user_id
has_attached_file :photo, :url => "/assets/:user/app/:class/:style_:basename.:
extension",
:path => "#{APP_PATH}/:user/app/:class/:style_:basename.:extension",
:styles => {
:thumb=> "50x50#",
:large => "400x" }
end
config/environment.rb
# Be sure to restart your server when you modify this file
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
config.time_zone = 'UTC'
config.active_record.observers = :user_observer #, :website_observer
config.gem "paperclip", :version => "~> 2.3"
end
SITE_NAME = 'Mydomain.com'
CMS_PREFIX = 'joomla'
config/environments.rb/staging.rb
# Settings specified here will take precedence over those in config/environment.rb
# The staging environment is meant for finished, apps and final acceptance testing.
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_view.cache_template_loading = true
ActionController::Base.asset_host = "staging.mydomain.com"
DOCUMENT_ROOT = "/var/www"
APP_PATH = "/u/apps"
DB_PREFIX = "a001"
DB_USER = "joomla"
DB_PWD = "*****"
GIT_URL = "[email protected]:****/mydomain.git"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => :plain,
:user_name => "****@gmail.com",
:password => "****"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将
validate_attachment_presence
行移到has_attached_file
块之后 -has_attached_file
包含 Paperclip 的实例方法,因此排序可能存在问题。我知道开发环境和生产环境之间的类加载有所不同,但我不确定这些差异的程度。Try moving the
validate_attachment_presence
line after thehas_attached_file
block -has_attached_file
includes Paperclip's instance methods so there could be a problem in the ordering. I know that class loading differs between dev and production environments, but I'm not sure of the extent of those differences.好的,我通过添加
到 image.rb 类解决了这个问题。
我现在就采用这个解决方法。
OK, I've solved this by adding
to the image.rb class.
I am going with this workaround for now.
您输入的验证方法有误。根据Paperclip Rdoc,正确的方法名称是
validate_attachment_presence
- 它是“验证”而不是“验证”。You mis-typed the validation method. According to the Paperclip Rdoc, the correct method name is
validate_attachment_presence
- it is "validate" not "validates".