我希望能够创建元类方法例如,我的网站上有一个自动完成机制来处理select_标记。
以下是我希望如何构建我的选择选项:

f.select(:subjects, options_from_collection_for_select(Subject.autocomplete_for_subject_title, :id, :title))

所以我的autocomplete_for_subject_title类方法可以返回for now subject.all
我考虑的是一个模块,而不是为给定的模型定义一个类方法,如下所示:
主题.rb
class Subject
  include SimpleAutocomplete

  field :title, :type => String

  autocomplete_for :subject, :title
end

lib/simple_自动完成.rb
module SimpleAutocomplete
  def self.included(base)
    base.class_eval do |klass|
      extend ClassMethods
    end
  end

  module ClassMethods
    def autocomplete_for(object, method, options = {}, &block)

      (class << object; object; end).instance_eval { define_method "autocomplete_for_#{object}_#{method}" do
          puts "It works!"
        end
      }

    end
  end
end

但当我启动rails服务器时,我得到的是:
simple_autocomplete.rb:13:in `autocomplete_for': can't define singleton (TypeError)
    from /Users/pierrelouisgottfrois/Work/teamento_beta_git/app/models/subject.rb:20:in `<class:Subject>'
    from /Users/pierrelouisgottfrois/Work/teamento_beta_git/app/models/subject.rb:1:in `<top (required)>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:454:in `load'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:454:in `block in load_file'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:453:in `load_file'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:300:in `depend_on'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:216:in `require_dependency'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:55:in `load_model'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:18:in `block (2 levels) in load_models'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:17:in `each'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:17:in `block in load_models'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/paths.rb:102:in `block in each'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/paths.rb:102:in `each'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/paths.rb:102:in `each'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:16:in `load_models'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/railtie.rb:86:in `block (2 levels) in <class:Railtie>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:33:in `new'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:33:in `build'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `each'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `inject'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `build'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application.rb:162:in `app'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application/finisher.rb:35:in `block in <module:Finisher>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `run'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `each'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application.rb:134:in `initialize!'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application.rb:77:in `method_missing'
    from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config/environment.rb:10:in `<top (required)>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
    from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config.ru:3:in `block in <main>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config.ru:1:in `new'
    from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config.ru:1:in `<main>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/server.rb:65:in `start'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:27:in `tap'
    from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

我已经阅读了这篇博文开始:http://blog.jayfields.com/2007/10/ruby-defining-class-methods.html
你知道吗?

最佳答案

您试图通过object调用class << object的单例类,但是object是符号:subject,我认为这不是您想要的。
我不知道你到底想要什么,介意吧,但那也许能帮你弄清楚。

10-07 17:20