本文介绍了使用 gem 添加辅助方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了很多关于添加表单辅助方法的信息(请参阅我的其他问题之一),但我找不到关于添加辅助方法的任何信息,就好像它们是在 application_helper.rb
.
I have found a lot of information about adding form helper methods (see one of my other questions), but I can't find anything about adding helper methods as if they were defined in application_helper.rb
.
我尝试将 application_helper.rb
从 rails 应用程序复制到 gem 中,但没有成功.
I've tried copying application_helper.rb
from a rails app into the gem but that didn't work.
我也试过:
class ActionView::Helpers
..但是会产生错误.
推荐答案
在某处为你的辅助方法创建一个模块:
Create a module somewhere for your helper methods:
module MyHelper
def mymethod
end
end
将其混入 ActionView::Base(例如在 init.rb
或 lib/your_lib_file.rb
中)
Mix it into ActionView::Base (such as in init.rb
or lib/your_lib_file.rb
)
ActionView::Base.send :include, MyHelper
这篇关于使用 gem 添加辅助方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!