سؤال

أقوم بإنشاء جوهرة 3.0.3 ولا يمكنني تشغيلها:

# attached.rb
module Attached
  require 'attached/railtie' if defined?(Rails)
  def self.include(base)
    base.send :extend, ClassMethods
  end
  module ClassMethods
    def acts_as_fail
    end
  end
end

# attached/railtie.rb
require 'attached'
require 'rails'

module Attached
  class Railtie < Rails::Railtie
    initializer 'attached.initialize' do
      ActiveSupport.on_load(:active_record) do
        ActiveRecord::Base.send :include, Attached
      end
    end
  end
end

انا حصلت undefined local variable or method 'acts_as_fail' عندما أضيف acts_as_fail لأي من بلدي ActiveRecord عارضات ازياء. الرجاء المساعدة! أنا محبط للغاية من هذا الرمز التافهة على ما يبدو! شكرًا!

هل كانت مفيدة؟

المحلول

أنت تحدد self.include (السطر الرابع لأسفل) ، عندما تكون الطريقة الصحيحة self.included.

نصائح أخرى

يمكنك تبسيط الكود باستخدام extend مباشرة:

# attached.rb
module Attached
  require 'attached/railtie' if defined?(Rails)
  def acts_as_fail
  end
end

# attached/railtie.rb
require 'attached'
require 'rails'

module Attached
  class Railtie < Rails::Railtie
    initializer 'attached.initialize' do
      ActiveSupport.on_load(:active_record) do
        ActiveRecord::Base.send :extend, Attached
      end
    end
  end
end

هذه قراءة جيدة:http://yehudakatz.com/2009/11/12/better-ruby-idioms/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top