How can I get from an ERB code block without rendering it?

Consider the following:

view.html.erb

<%= make_backwards do %> stressed <% end %> 

helper.rb

 def make_backwards yield.reverse end 

The view displays stresseddesserts instead of stresseddesserts . How to use content in yield without rendering a block of code?

+6
source share
2 answers

ERB has an internal buffer that makes using blocks a bit more complicated, as you can see in your code example.

Rails provides a capture method that allows you to write a string inside this buffer and return it from a block.

So, your assistant will be as follows:

 def make_backwards capture do yield.reverse end end 
+4
source

You can try ff:

Option 1:

 <%= make_backwards { "stressed" } %> 

Option 2:

 <%= make_backwards do %> <% "stressed" %> <% end %> 

Let me know if this helps.

+1
source

All Articles