问题描述
我正在运行 Rails 3.2 和最新版本的 Authlogic.当我在 Mac 本地运行我的应用程序时,它运行良好.当我尝试在我的生产服务器(带有Passenger/Apache 的Ubuntu)上运行它时,我得到了这个:
I'm running Rails 3.2 and the latest version of Authlogic. When I run my app locally on my Mac, it works fine. When I try to run it on my production server (Ubuntu with Passenger/Apache), I get this:
You must establish a database connection before using acts_as_authentic
我不知道如何解决这个问题.我今天早些时候发布了这个相关问题,然后才意识到问题比我想象的要广泛.
I'm not sure how to troubleshoot the problem. I posted this related question earlier today before I realized that the problem was broader than I thought.
推荐答案
我找到了问题所在.看看来自 Authlogic 的 lib/authlogic/acts_as_authentic/base.rb
的这个片段:
I figured out the problem. Look at this snippet from Authlogic's 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
:
If column_names
throws an error, db_setup?
will return false. Look at this other function, also from 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
抛出的异常不同.
If db_setup?
returns false, Authlogic will throw an exception, but not the same exception thrown by column_names
.
我的问题是 column_names
抛出了这个异常:
My problem was that column_names
was throwing this exception:
/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 问题就消失了.
And the reason for THAT exception is that my user table is called user
, not users
, but Rails was not picking up on my pluralize_table_names
setting properly. Once I fixed my pluralize_table_names
problem (apparently the way this setting works has been changed in Rails 3.1), my Authlogic problem went away.
因此,如果您遇到此问题,不妨试试这个:
So if you're having this problem, you might want to try this:
- 将 Authlogic 存储库克隆到开发机器上的某个位置
- 更改您的 Gemfile 以使用本地版本的 Authlogic (
'authlogic', :path => '/path/to/authlogic'
) - 在
begin
/rescue
/end
之外添加对db_setup?
的column_names
调用代码>子句 - 看看你是否得到了一个不同的、可能更准确和信息量更大的错误,就像我一样
- Clone the Authlogic repo to somewhere on your dev machine
- Change your Gemfile to use the local version of Authlogic (
'authlogic', :path => '/path/to/authlogic'
) - Add a
column_names
call todb_setup?
outside thebegin
/rescue
/end
clause - See if you get a different, potentially more accurate and informative, error, like I did
这篇关于Authlogic 不适用于我的 Rails 3.2 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!