No output from ruby block when return value in method
Here's a trimmed down version of my code:
Helper method:
def render_tree nodes, &block
bloc开发者_运维百科k.call nodes[1]
return 0
end
and my HAML file:
= render_tree @sections do |node|
- logger.debug("BLOCK CALLED");
= node.title
The code should print node.title, with the method returning 0. Instead it just prints "0" (the return value).
If I comment out return 0
node.title is printed.
In both situations, the debugger prints "BLOCK CALLED".
I want to print "test" and return 0. What am I doing wrong here?
Edit: So I figure HAML is outputting whatever is returned from the method. The full example is a recursive method that prints out a bunch of stuff. How can I get it to process the HAML instead of just return it?
For starters, if you don't want to print 0(the return value), you have to change this line:
= render_tree @sections do |node|
to this line:
- render_tree @sections do |node|
I am not 100% on what you are asking yet...
But, if you want the render_tree method to process the HAML string contained in nodes[1] then you would want to do something like
def render_tree(nodes, &block)
html = Haml::Engine.new(nodes[1]).render.html_safe
block.call(html)
end
But, I don't think this will do quite what you are looking for. In part I am confused because your construction doesn't seem very "railsish."
Perhaps with a bit more context it would be clearer?
精彩评论