如何從另一個helper方法內部調用Rails helper方法?

[英]How do I call a Rails helper method from inside another helper method?


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!

希望這可以幫助!


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/08/09/7299084e4dd910b01622e4fede74b8e1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com