【问题标题】:Authlogic doesn't work with my Rails 3.2 appAuthlogic 不适用于我的 Rails 3.2 应用程序
【发布时间】:2012-03-01 15:31:15
【问题描述】:

我正在运行 Rails 3.2 和最新版本的 Authlogic。当我在我的 Mac 上本地运行我的应用程序时,它运行良好。当我尝试在我的生产服务器(带有Passenger/Apache的Ubuntu)上运行它时,我得到了这个:

You must establish a database connection before using acts_as_authentic

我不确定如何解决问题。我今天早些时候发布了this related question,然后才意识到问题比我想象的要广泛。

【问题讨论】:

  • 我曾经是 authlogic 的忠实粉丝,在 2.x 时代有很多应用程序使用它(包括 LDPA、RPX 等适配器)。我已经将其中的大部分升级到了 3.0.x 和 3.2,但在这个过程中,我决定总体而言它更好、更容易迁移到设计中。这很简单,而且代码更干净(通常是由于是在 3.0 时代设计的)。这并不能回答您的问题..我只是提到它,因此如果 authlogic 一直在反击,请记住它;切换到设计并不是什么大不了的事,而且你不需要很长时间就能掌握基础知识。
  • 前几天我终于改用了 Devise。这是一个非常简单和无痛的切换。
  • 这似乎已修复。现在我们需要在 gemfile 中使用 gem 'authlogic', :git => 'git://github.com/binarylogic/authlogic.git' 从 master 分支中提取。

标签: passenger ruby-on-rails-3.2


【解决方案1】:

我发现了问题所在。从 Authlogic 的lib/authlogic/acts_as_authentic/base.rb 看这个 sn-p:

    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')
  • begin/rescue/end 子句之外添加对db_setup?column_names 调用
  • 看看你是否像我一样遇到了不同的、可能更准确、信息更丰富的错误

【讨论】:

  • 当然,如果 Authlogic 意识到这个问题的可能性,那就太好了。我发布了一个新问题,询问如何检测这个特定问题,以便我可以对 Authlogic 应用补丁......假设这是一个好主意。 stackoverflow.com/questions/9557860/…
【解决方案2】:

我已经在我的叉子上解决了这个问题。在 Ben 有时间合并修复之前,您可以使用 Gemfile 中的固定分支解决此问题;

gem 'authlogic', :git => 'git@github.com:james2m/authlogic.git', :branch => 'fix-migrations'

【讨论】:

    【解决方案3】:

    对于任何可能来到此页面寻找答案的人。

    一个原因可能是您尚未创建测试数据库。

    只要运行:

    $ RAILS_ENV=test rake db:create db:migrate

    【讨论】:

      【解决方案4】:

      关注https://github.com/binarylogic/authlogic/issues/318 和 +1 上的未解决问题,以便尽快合并修复:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-13
        • 2017-09-14
        • 1970-01-01
        相关资源
        最近更新 更多