Reputation: 1401
Consider the following:
view.html.erb:
<%= make_backwards do %>
stressed
<% end %>
helper.rb:
def make_backwards
yield.reverse
end
The view renders stresseddesserts
instead of just desserts
. How do I use the content in yield
without rendering the code block?
Upvotes: 7
Views: 1380
Reputation: 32629
ERB has an internal buffer, which makes using blocks a bit more complicated, as you can see in your code example.
Rails provides a capture method, which allows you to capture a string inside this buffer and return it from a block.
So your helper would become the following:
def make_backwards
capture do
yield.reverse
end
end
Upvotes: 5
Reputation: 5644
You could try doing the ff:
Option 1:
<%= make_backwards { "stressed" } %>
Option 2:
<%= make_backwards do %>
<% "stressed" %>
<% end %>
Let me know if it helps.
Upvotes: 1