我正在使用带有 Authlogic 3.0.3 的 Rails 3.1。我在 Heroku 上部署了我的应用程序。在本地,一切正常,但是,当我部署到 Heroku 时,出现以下错误:
ArgumentError (wrong number of arguments (3 for 2)):
app/controllers/application_controller.rb:38:in `current_user_session'
app/controllers/application_controller.rb:43:in `current_user'
app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
使用更完整的堆栈跟踪:
vendor/plugins/rpm/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb:16:in `log_with_newrelic_instrumentation'
.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:550:in `exec_query'
.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:679:in `table_exists?'
.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/attribute_methods/primary_key.rb:41:in `get_primary_key'
.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/attribute_methods/primary_key.rb:25:in `reset_primary_key'
.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/attribute_methods/primary_key.rb:16:in `primary_key'
.bundle/gems/ruby/1.9.1/gems/authlogic-3.0.3/lib/authlogic/session/session.rb:48:in `session_credentials'
.bundle/gems/ruby/1.9.1/gems/authlogic-3.0.3/lib/authlogic/session/session.rb:33:in `persist_by_session'
.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:413:in `_run_persist_callbacks'
.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
.bundle/gems/ruby/1.9.1/gems/authlogic-3.0.3/lib/authlogic/session/callbacks.rb:90:in `persist'
.bundle/gems/ruby/1.9.1/gems/authlogic-3.0.3/lib/authlogic/session/persistence.rb:55:in `persisting?'
.bundle/gems/ruby/1.9.1/gems/authlogic-3.0.3/lib/authlogic/session/persistence.rb:39:in `find'
app/controllers/application_controller.rb:38:in `current_user_session'
app/controllers/application_controller.rb:43:in `current_user'
app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
我有 Authlogic 设置,因为它在我需要的示例中显示,但简单的身份验证。这是我的 ApplicationController 中的代码:
class ApplicationController < ActionController::Base
protect_from_forgery
layout 'application'
before_filter { |c| Authorization.current_user = current_user } #this is line 5
helper_method :current_user, :current_user_session
# erroneous stuff removed
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find #line 43
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user #line 38
end
end
我的用户会话:
class UserSession < Authlogic::Session::Base
find_by_login_method :find_by_anything
end
调用用户中的方法:
class User < ActiveRecord::Base
attr_accessible :email, :password, :remember_me
acts_as_authentic do |c|
c.login_field = 'name'
c.require_password_confirmation = false
c.validates_format_of_login_field_options = {:with => /^[a-zA-Z0-9]+$/, :message => "should use only letters and numbers, no other characters."}
c.validates_length_of_login_field_options = {:within => 5..25}
end
def self.find_by_anything(login)
# Postgres and SQLite use a case sensitive search, so we need to make it case insensitive
find_by_smart_case_login_field(login) || find_by_email(login)
end
我的猜测是 UserSession.find 是罪魁祸首,但我无法终生解决这个问题。我已经尝试了两天,但日志不是很有帮助,而且我似乎无法浏览源代码。环境之间的唯一区别是我在本地使用 MySQL,而 heroku 使用 Postgres。
EDIT: the issue was not Authlogic, it was NewRelic. It just happened that the error would bubble up and become apparent through Authlogic -- see answer for solution.