2014-07-04 32 views
-1

浏览器错误:未定义的方法:connect_timeout

NoMethodError 
undefined method `connect_timeout=' for #<Mysql2::Client:0x47f7570> 

在我的浏览器,错误出现的是connect_timeout是不确定的。我很确定它与client.rb文件有关。我会告诉你这个文件。我不得不编辑一些它才能真正让Webrick运行起来。当我启动服务器时,除非我做出更改,否则我的命令行上总会出现一个错误。我评论过我编辑的内容。有时编辑随机的东西,其中一些工作,但他们在我的浏览器产生不同的错误。我正在使用Windows 8机器。感谢您的帮助。

module Mysql2 
class Client 
attr_reader :query_options, :read_timeout 
@@default_query_options = { 
    :as => :hash,     # the type of object you want each row back as; also supports :array (an array of values) 
    :async => false,    # don't wait for a result after sending the query, you'll have to monitor the socket yourself then eventually call Mysql2::Client#async_result 
    :cast_booleans => false,  # cast tinyint(1) fields as true/false in ruby 
    :symbolize_keys => false,  # return field names as symbols instead of strings 
    :database_timezone => :local, # timezone Mysql2 will assume datetime objects are stored in 
    :application_timezone => nil, # timezone Mysql2 will convert to before handing the object back to the caller 
    :cache_rows => true,   # tells Mysql2 to use it's internal row cache for results 
    #:connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION, 
    #I had to delete the line above because for some reason the command prompt said that each of the constants were undefined were not used in the right place or something 
    :cast => true, 
    :default_file => nil, 
    :default_group => nil 
} 

def initialize (opts = {}) 
    opts = Mysql2::Util.key_hash_as_symbols(opts) 
    @read_timeout = nil 
    @query_options = @@default_query_options.dup 
    @query_options.merge! opts 

    #initialize_ext 
    # the chrome page said that the above variable is undefined :P 

    # Set default connect_timeout to avoid unlimited retries from signal interruption 
    opts[:connect_timeout] = 120 unless opts.key?(:connect_timeout) 

    [:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth, :init_command].each do |key| 
    next unless opts.key?(key) 
    case key 
    when :reconnect, :local_infile, :secure_auth 
     send(:"#{key}=", !!opts[key]) 
    when :connect_timeout, :read_timeout, :write_timeout 
     send(:"#{key}=", opts[key].to_i) 
    else 
     send(:"#{key}=", opts[key]) 
    end 
    end 

    # force the encoding to utf8 
    self.charset_name = opts[:encoding] || 'utf8' 

    ssl_options = opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslcipher) 
    ssl_set(*ssl_options) if ssl_options.any? 

    if [:user,:pass,:hostname,:dbname,:db,:sock].any?{|k| @query_options.has_key?(k) } 
    warn "============= WARNING FROM mysql2 =============" 
    warn "The options :user, :pass, :hostname, :dbname, :db, and :sock will be deprecated at some point in the future." 
    warn "Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options." 
    warn "============= END WARNING FROM mysql2 =========" 
    end 

    user  = opts[:username] || opts[:user] 
    pass  = opts[:password] || opts[:pass] 
    host  = opts[:host] || opts[:hostname] 
    port  = opts[:port] 
    database = opts[:database] || opts[:dbname] || opts[:db] 
    socket = opts[:socket] || opts[:sock] 
    flags = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags] 

    # Correct the data types before passing these values down to the C level 
    user = user.to_s unless user.nil? 
    pass = pass.to_s unless pass.nil? 
    host = host.to_s unless host.nil? 
    port = port.to_i unless port.nil? 
    database = database.to_s unless database.nil? 
    socket = socket.to_s unless socket.nil? 

    connect user, pass, host, port, database, socket, flags 
end 

def self.default_query_options 
    @@default_query_options 
end 

def query_info 
    info = query_info_string 
    return {} unless info 
    info_hash = {} 
    info.split.each_slice(2) { |s| info_hash[s[0].downcase.delete(':').to_sym] = s[1].to_i } 
    info_hash 
end 

private 
    def self.local_offset 
    ::Time.local(2010).utc_offset.to_r/86400 
    end 

end 
end 
+0

你能简单解释一下为什么你这么做吗? –

回答

0

Mysql2::Client#initialize称为connect_timeout=但没有在客户端,attr_writer。

when :connect_timeout, :read_timeout, :write_timeout 
    send(:"#{key}=", opts[key].to_i) 
else 

如果该客户端通过自己编写的,加入Mysql2::Client的定义attr_accessor :connect_timeout和合理利用的属性。如果来自其他库,请检查您的加载路径。您可能错过了一些打开Mysql2::Client的文件,并且猴子会对其进行修补。

+0

谢谢你的回答。我很抱歉,但我对计算机编程颇为陌生。你能解释一下你的答案中每个术语的含义吗?我不确定mysql是如何处理这些东西的。那么客户端的attr_writers是什么?并且我复制粘贴下面给出的代码,因为我似乎无法在client.rb中找到它。另外,我认为rails上的ruby为我生成了这个页面,但命令行不断让我改变它来启动web服务器。另外,我刚刚在计算机上搜索client.rb,这就是我找到它的原因。感谢您的帮助。 – xoshi827