Authlogic doesn't work with my Rails 3.2 app

最后都变了- 提交于 2019-11-30 20:12:34

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

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

If db_setup? returns false, Authlogic will throw an exception, but not the same exception thrown by 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

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:

  • 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 to db_setup? outside the begin/rescue/end clause
  • See if you get a different, potentially more accurate and informative, error, like I did

I've fixed this on my fork. Until Ben has time to merge the fix you can work around this using the fixed branch in your Gemfile;

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

For anyone else who might have come to this page looking for an answer.

One reason could be that your haven't created your test database.

Just run:

$ RAILS_ENV=test rake db:create db:migrate

Follow the open issue at https://github.com/binarylogic/authlogic/issues/318 and +1 so the fix gets merged soon :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!