2010-11-06 57 views

回答

57

你还可以尝试:

YourController.helpers.my_helper 

例如:

> ApplicationController.helpers.content_tag(:div, "test") 
=> "<div>test</div>" 

这适用于固定导轨2和Rails 3

+0

我该如何渲染偏色? Rails 4.'ApplicationController.helpers.render Post.last NoMethodError:undefined method'include?' for nil:NilClass' – Chloe 2014-02-28 02:53:24

+0

> MyController.helpers.my_method(arg1,arg2);这是我正在寻找的。 – etlds 2014-05-28 15:19:40

+0

谢谢..有用。 – 2015-02-16 07:05:06

17

请尝试这个

rails console 
helper.any_method_of_helper(pass_argument) 

示例

helper.number_to_currency('123.45') 
+0

这工作。然而,还有一种情况是,如果你已经在嵌套在模块内部的文件中定义了你的帮助器方法,例如'MyModule :: ModuleSpecificViewHelper'(在'/ app/helpers/my_module/module_specific_view_helper.rb'处定义),那么在rails控制台中应该首先包含该模块(如果尚未包含的话),然后尝试'helper.your_method',你应该得到它。 – 2017-11-22 10:42:15

相关问题