Rails - 如何在控制器内使用助手
虽然我意识到您应该在视图中使用助手,但我在控制器中需要一个助手,因为我正在构建要返回的 JSON 对象。
它有点像这样:
def xxxxx
@comments = Array.new
@c_comments.each do |comment|
@comments << {
:id => comment.id,
:content => html_format(comment.content)
}
end
render :json => @comments
end
如何访问我的 html_format
帮助程序?
While I realize you are supposed to use a helper inside a view, I need a helper in my controller as I'm building a JSON object to return.
It goes a little like this:
def xxxxx
@comments = Array.new
@c_comments.each do |comment|
@comments << {
:id => comment.id,
:content => html_format(comment.content)
}
end
render :json => @comments
end
How can I access my html_format
helper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以
helpers.
(或ActionController::Base.helpers.
)view_context.
(Rails 4 & 3)(警告:这会在每次调用时实例化一个新的视图实例)@template.
(Rails 2)singleton.helper
包含
帮助器(警告:将使所有帮助器方法转化为控制器动作)You can use
helpers.<helper>
in Rails 5+ (orActionController::Base.helpers.<helper>
)view_context.<helper>
(Rails 4 & 3) (WARNING: this instantiates a new view instance per call)@template.<helper>
(Rails 2)singleton.helper
include
the helper in the controller (WARNING: will make all helper methods into controller actions)注意: 这是在 Rails 中编写并接受的 2 天;现在grosser的答案是要走的路。
选项 1: 可能最简单的方法是将辅助模块包含在控制器中:
选项 2: 或者您可以将辅助方法声明为类函数,然后像这样使用它: so:
如果您希望能够将它用作实例函数和类函数,您可以在助手中声明这两个版本:
Note: This was written and accepted back in the Rails 2 days; nowadays grosser's answer is the way to go.
Option 1: Probably the simplest way is to include your helper module in your controller:
Option 2: Or you can declare the helper method as a class function, and use it like so:
If you want to be able to use it as both an instance function and a class function, you can declare both versions in your helper:
在 Rails 5 中,请在控制器中使用
helpers.helper_function
。示例:
来源:来自 @Markus 对不同答案的评论。我觉得他的答案应该有自己的答案,因为它是最干净、最简单的解决方案。
参考:https://github.com/rails/rails/pull/24866
In Rails 5 use the
helpers.helper_function
in your controller.Example:
Source: From a comment by @Markus on a different answer. I felt his answer deserved it's own answer since it's the cleanest and easier solution.
Reference: https://github.com/rails/rails/pull/24866
我的问题通过选项 1 解决。可能最简单的方法是将辅助模块包含在控制器中:
My problem resolved with Option 1. Probably the simplest way is to include your helper module in your controller:
一般来说,如果助手要在(仅)控制器中使用,我更喜欢将其声明为
class ApplicationController
的实例方法。In general, if the helper is to be used in (just) controllers, I prefer to declare it as an instance method of
class ApplicationController
.在 Rails 5 之前,您必须包含辅助模块。
在较新的版本中,您可以将控制器中的助手与助手(复数)对象一起使用。
https://www.rubyguides.com/2020/01/rails-helpers/
Before Rails 5, you have to include the helper module.
In newer versions, you can use helpers in your controller with the helpers (plural) object.
https://www.rubyguides.com/2020/01/rails-helpers/
在 Rails 5+ 中,您可以简单地使用该函数,如下面的简单示例所示:
In Rails 5+ you can simply use the function as demonstrated below with simple example:
在 Rails 6 中,只需将其添加到控制器中:
现在 user_helpers.rb 将在控制器中可用。
In rails 6, simply add this to your controller:
Now the user_helpers.rb will be available in the controller.
其他答案中缺少的一个替代方案是您可以采取相反的方式:在控制器中定义您的方法,然后使用 helper_method 使其也可以作为辅助方法在视图上使用。
例如:
One alternative missing from other answers is that you can go the other way around: define your method in your Controller, and then use helper_method to make it also available on views as, you know, a helper method.
For instance:
在 Rails 5+ 中,只需使用 helpers (helpers.number_to_currency(10000))
In Rails 5+ simply use helpers (helpers.number_to_currency(10000))