Reputation: 7210
How can I pass a block to a method. For example:
# in the view
button(link) do
'<div>html content</div>'
end
def button(&block)
block
end
I have the following code which is working:
# in helper file
def button(&block)
link_to '/' do
block.call.html_safe
end
end
# in the view
button {'<div>html content</div>'}
Upvotes: 0
Views: 653
Reputation: 121000
You are likely messing things up. What you are trying to achieve is easily achievable without any yield
magic, with plain old good method parameter:
def button html
link_to '/', html.html_safe
end
button '<div>html content</div>'
If one wants to yield a value from a block, do something with it and pass it further, one might:
def button
raise unless block_given?
λ = Proc.new
link_to '/', &-> { λ.call.html_safe }
end
button { '<div>html content</div>' }
Upvotes: 2
Reputation: 230336
So, you just want to pass the block on to link_to? Do this.
def button(&block)
link_to '/', &block
end
Upvotes: 3
Reputation: 2775
use yield:
def new_button(link)
link_to link do
yield.html_safe if block_given?
end
end
Upvotes: 1