如何从另一个helper方法内部调用Rails helper方法?

时间:2022-06-01 17:01:02

I am writing a helper that needs to call another helper, which generates html. How do I do that?

我正在编写一个助手,需要调用另一个助手,它生成html。我该怎么做呢?

3 个解决方案

#1


15  

try:
include AnotherHelper

试一试:包括AnotherHelper

#2


5  

Just call it.

就叫它。

If it is in a different helper file, your controller can include the other helpfile by using the controller method "helper"

如果它在另一个帮助文件中,您的控制器可以使用controller方法“helper”包含其他帮助文件

Added:

补充道:

Here is an example:

这是一个例子:

# in the view
<%= my_helper %>

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

** Please add more details to your question if this isn't helping....

* *请添加更多的细节,你的问题如果这不是帮助....

#3


1  

Something like this should help you (say, in application_helper.rb)

类似这样的内容应该可以帮助您(例如,在application_helper.rb中)

module ApplicationHelper

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

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

end

In this case, the create_div method is calling the html method with a string as an argument. the html method returns a string of HTML with the argument you supply embedded. in a view, it would look like:

在本例中,create_div方法使用字符串作为参数调用html方法。html方法返回一个包含您提供的参数的html字符串。从某种角度来看,它看起来是:

<%= create_div %>

hope this helps!

希望这可以帮助!

#1


15  

try:
include AnotherHelper

试一试:包括AnotherHelper

#2


5  

Just call it.

就叫它。

If it is in a different helper file, your controller can include the other helpfile by using the controller method "helper"

如果它在另一个帮助文件中,您的控制器可以使用controller方法“helper”包含其他帮助文件

Added:

补充道:

Here is an example:

这是一个例子:

# in the view
<%= my_helper %>

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

** Please add more details to your question if this isn't helping....

* *请添加更多的细节,你的问题如果这不是帮助....

#3


1  

Something like this should help you (say, in application_helper.rb)

类似这样的内容应该可以帮助您(例如,在application_helper.rb中)

module ApplicationHelper

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

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

end

In this case, the create_div method is calling the html method with a string as an argument. the html method returns a string of HTML with the argument you supply embedded. in a view, it would look like:

在本例中,create_div方法使用字符串作为参数调用html方法。html方法返回一个包含您提供的参数的html字符串。从某种角度来看,它看起来是:

<%= create_div %>

hope this helps!

希望这可以帮助!