如果我想创建一个新的Rails宝石(gem),以向Rails视图添加方法,正确的做法是什么?是扩展ActionView::Base吗?是否涉及ApplicationHelper?
如果我想创建一个新的Rails宝石(gem),以向Rails视图添加方法,正确的做法是什么?是扩展ActionView::Base吗?是否涉及ApplicationHelper?
ActionView::Base
中。module MyGem
module ActionViewExtensions
module MyHelpers
def my_view_helper
# ...
end
end
end
end
# You can do this here or in a Railtie
ActionView::Base.send :include, MyGem::ActionViewExtensions::MyHelpers
https://github.com/mynameisrufus/sorted/blob/master/lib/sorted/railtie.rb
替代方案: