我在中有一个文件lib/thing.rb
,在那儿我需要调用ApplicationHelper
在中的类方法中定义的方法thing.rb
换句话说,Thing.some_method()
它format_me()
在ApplicationHelper
我已经尝试过所有可以想到的方式,extend ApplicationHelper
或者extend ActionView::Helpers::ApplicationHelper
一直在说未初始化的常量。香港专业教育学院也尝试过,Rails.application.helpers
但我不知道如何访问那里的方法...
如果您收到有关未初始化的常量的错误,那么你可以通过可能解决问题移动lib/
到app/
。给定以下文件和布局,使用Ruby 2.6.3和Rails 5.2.3:
# app/helpers/application_helper.rb
module ApplicationHelper
def baz
SecureRandom.uuid
end
end
和:
# app/lib/foo.rb
class Foo
extend ApplicationHelper
def self.bar
baz
end
end
回电Foo.bar
:
=> "b6322675-47b2-4ae8-a19a-ffd4af8d6f84"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句