简体   繁体   中英

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?

try:
include AnotherHelper

Just call it.

If it is in a different helper file, your controller can include the other helpfile by using the controller method " 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....

Something like this should help you (say, in 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 %>

hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM