16

我正在编写一个需要调用另一个帮助器的帮助器,它会生成 html。我怎么做?

4

3 回答 3

18

尝试:
包括另一个助手

于 2010-08-11T03:52:48.390 回答
5

就叫吧。

如果它在不同的帮助文件中,您的控制器可以通过使用控制器方法“ helper ”包含其他帮助文件

添加:

这是一个例子:

# in the view
<%= my_helper %>

# in the helper file
def my_helper
  "<div>" + someother_helper_which_generates_html + "</div>"
end

** 如果这没有帮助,请在您的问题中添加更多详细信息......

于 2010-08-09T03:19:44.753 回答
0

像这样的东西应该可以帮助你(比如,在 application_helper.rb 中)

module ApplicationHelper

  def create_div
    html("this is some content")
  end

  def html(content)
    "<div>#{content}</div>"
  end

end

在这种情况下,create_div 方法正在调用带有字符串作为参数的 html 方法。html 方法返回一个 HTML 字符串,其中包含您提供的嵌入参数。在一个视图中,它看起来像:

<%= create_div %>

希望这可以帮助!

于 2010-08-11T04:52:11.520 回答