我正在迁移使用mySQL数据库后端的rails应用程序,并要求用户登录数据库。当用户登录无效时,mySQL驱动程序会引发预期的ActiveRecord :: JDBCError异常。我通过' rescue_from'来抓住这个例外。在我尝试重定向到另一个页面的ApplicationController中。看起来好像被捕获并且重定向正在发生,但是"内部服务器错误"显示页面而不是我尝试重定向到的页面。我只是用WEBrick服务器测试它。
以下是相关细节:
Ruby版本: jruby 1.7.13(1.9.3p392)2014-06-24 43f133c在OpenJDK 64位服务器VM 1.7.0_55-mockbuild_2014_04_09_11_51-b00 [linux-amd64]
Rails版本: Rails 4.1.1
application_controller.rb:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
rescue_from ActiveRecord::JDBCError do |exception|
redirect_to :controller=>"/account", :action =>"logout
end
end
退出&#39;方法就是
def logout
session['user'] = nil
end
服务器命令行输出:
Started POST "/login" for 10.196.145.220 at 2014-08-27 10:57:12 -0700
Processing by AccountController#login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9uE89hmHsmA94u+ItzNldTeEa8nWyNmLuxoifhTN7eY=", "user_login"=>"", "user_password"=>"[FILTERED]", "commit"=>"Login"}
Redirected to http://server-1:3000/logout
Completed 302 Found in 46ms
[2014-08-27 10:57:12] ERROR ActiveRecord::JDBCError: The driver encountered an unknown error: java.sql.SQLException: Access denied for user ''@'server.test.org' (using password: NO)
/usr/local/rvm/gems/jruby-1.7.13/gems/activerecord-jdbc-adapter-1.3.9/lib/arjdbc/jdbc/connection.rb:23:in `initialize'
/usr/local/rvm/gems/jruby-1.7.13/gems/activerecord-jdbc-adapter-1.3.9/lib/arjdbc/jdbc/adapter.rb:68:in `initialize'
/usr/local/rvm/gems/jruby-1.7.13/gems/activerecord-jdbc-adapter-1.3.9/lib/arjdbc/mysql/adapter.rb:658:in `initialize'
....