虽然我意识到你应该在视图中使用一个帮助器,但我需要在我的控制器中使用一个帮助器,因为我正在构建一个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助手?
在Rails 5中使用helper。控制器中的Helper_function。
例子:
def update
# ...
redirect_to root_url, notice: "Updated #{helpers.pluralize(count, 'record')}"
end
来源:来自@Markus关于不同答案的评论。我觉得他的答案应该有自己的答案,因为这是最干净、最简单的解决方案。
参考:https://github.com/rails/rails/pull/24866
在Rails 5中使用helper。控制器中的Helper_function。
例子:
def update
# ...
redirect_to root_url, notice: "Updated #{helpers.pluralize(count, 'record')}"
end
来源:来自@Markus关于不同答案的评论。我觉得他的答案应该有自己的答案,因为这是最干净、最简单的解决方案。
参考:https://github.com/rails/rails/pull/24866