Ruby-on-rails 我如何在不同的视图中使用助手
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6071607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i use a helper in different views
提问by Hans
i am using refinery cms at the moment. I created an engine and with it some helpers in app/helpers/admin/.
now i would like to use those helpers in my frontend view (ie. app/views/myapp/index) as well. but i can not...undefined methode error.
what do i have to do short of copying the whole thing to app/helpers/?
the helper looks like this
我目前正在使用炼油厂 cms。我创建了一个引擎,并在app/helpers/admin/. 现在我也想在我的前端视图(即app/views/myapp/index)中使用这些助手。但我不能......未定义的方法错误。除了将整个内容复制到,我还需要做app/helpers/什么?助手看起来像这样
module Admin
module myHelper
def somefunc
end
end
end
so is it possible to use somefuncoutside of the Admin module?
那么是否可以somefunc在管理模块之外使用?
采纳答案by d11wtq
In your application_helper.rb:
在您的application_helper.rb:
module ApplicationHelper
include Admin::MyHelper
end
This will import those helper methods into the ApplicationHelper, thus making them available in your views. You could do this in any of your helpers really.
这会将这些辅助方法导入到 中ApplicationHelper,从而使它们在您的视图中可用。你真的可以在你的任何一个助手中做到这一点。
回答by bobics
The "Rails way" to include a helper from a non-standard path in a view is to use the .helper method within your controller.
在视图中包含来自非标准路径的助手的“Rails 方式”是在控制器中使用 .helper 方法。
class MyController < ApplicationController
helper Admin::MyHelper
...
end
http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper
http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper
回答by arnep
You might try to use the full object reference like Admin::myHelper::somefuncto call somefuncfrom outside the Adminmodule.
您可能会尝试使用完整的对象引用,例如从模块外部Admin::myHelper::somefunc调用。somefuncAdmin

