我正在运行Rails 3.2和最新版本的Authlogic。当我在Mac上本地运行应用程序时,它运行良好。当我尝试在生产服务器(Ubuntu,Passenger/Apache)上运行它时,得到以下信息:
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', :path => '/path/to/authlogic'
)本地版本column_names
/db_setup?
/begin
子句之外的rescue
中添加end
调用关于passenger - Authlogic不适用于我的Rails 3.2应用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9518976/