I’m using Rails 3.0.3, and the following template (with a .html.erb extension):
<% "one"; "two"; capture do %>
Three
<% end %>
Is rendering as:
one
Why is this? It doesn’t seem like it should render anything, since I didn’t use <%=
EDIT
Since there seems to be some confusion, here is a reproduction that more closely resembles the actual template code that I’m debugging:
<% my_string = "" %>
<% my_string << capture do %>
Hello
<% end %>
<%= my_string %>
This is rendering as:
Hello Hello
Because for some reason, the captured output is being appended to my_string AND being rendered, instead of just the former.
You’re using the rails capture helper but your syntax is wrong. I don’t know why it’s printing out
one. In rails 3.4 it doesn’t print out anything.Here’s the right way to use it
The html that will be rendered is
Since your syntax is wrong I wouldn’t worry about why “one” is showing up. Instead I’d focus more on using the capture helper correctly.
Edit:
Your second (edited) example is not the same as your first one. Here it is with line numbers and a slight change to the last line so you can see what’s going on.
The result is
Hello before Hello after. So line 3 is being rendered, then line 5 is being rendered withmy_stringcontaining the valueHello.If you change it to this
Then the result is
before Hello after.So what does this all mean? When you use
<<it’s screwing up thecapturemethod and it’s rendering stuff that’s inside the capture block even though normally you don’t expect it to.Basically, you can’t do what you’re trying to do here, at least not with your current syntax.