我正在运行 Rails 3.2 和最新版本的 Authlogic。当我在 Mac 上本地运行应用程序时,它运行良好。当我尝试在我的生产服务器(带有 Passenger/Apache 的 Ubuntu)上运行它时,我得到以下信息:
You must establish a database connection before using acts_as_authentic
我不知道如何解决该问题。我发了this related question今天早些时候,我意识到问题比我想象的要广泛。
最佳答案
我发现了问题所在。查看 Authlogic 的 lib/authlogic/acts_as_authentic/base.rb
中的这段代码片段:
private
def db_setup?
begin
column_names
true
rescue Exception
false
end
end
如果column_names
抛出错误,db_setup?
将返回 false。看看这个另一个函数,同样来自 base.rb
:
def acts_as_authentic(unsupported_options = nil, &block)
# Stop all configuration if the DB is not set up
raise StandardError.new("You must establish a database connection before using acts_as_authentic") if !db_setup?
raise ArgumentError.new("You are using the old v1.X.X configuration method for Authlogic. Instead of " +
"passing a hash of configuration options to acts_as_authentic, pass a block: acts_as_authentic { |c| c.my_option = my_value }") if !unsupported_options.nil?
yield self if block_given?
acts_as_authentic_modules.each { |mod| include mod }
end
如果db_setup?
返回 false,Authlogic 将抛出异常,但与 column_names
抛出的异常不同。
我的问题是 column_names
抛出此异常:
/Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1106:in `async_exec': PG::Error: ERROR: relation "users" does not exist (ActiveRecord::StatementInvalid)
LINE 4: WHERE a.attrelid = '"users"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
出现该异常的原因是我的用户表名为 user
,不是users
,但 Rails 没有接收到我的 pluralize_table_names
设置正确。一旦我修复了我的pluralize_table_names
问题(显然这个设置的工作方式在 Rails 3.1 中已经改变),我的 Authlogic 问题就消失了。
因此,如果您遇到此问题,您可能想尝试以下操作:
- 将 Authlogic 存储库克隆到您的开发计算机上的某个位置
- 更改您的 Gemfile 以使用本地版本的 Authlogic (
'authlogic', :path => '/path/to/authlogic'
) - 添加
column_names
调用db_setup?
外begin
/rescue
/end
条款 - 看看您是否会像我一样遇到不同的、可能更准确、信息更丰富的错误
关于passenger - Authlogic 不适用于我的 Rails 3.2 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518976/