I have test method in helpers/application_helper.rb file:
def test
concat("Hello world")
end
Then, in index.html.erb I call as:
Blah
<% test %>
The browser display:
Blah
Hello world
It’s normally, but if I change
<%= test %>
the browser display:
Blah Hello worldBlah Hello world
It duplicate all of page. I don’t know why?
What difference between them?
Thanks for your help!
From the Rails docs,
concatis only supposed to be used within a<% %>code block. When you use it in a<%= %>code block, you see it twice becauseconcatappends the provided text to the output buffer, but then it also returns the entire output buffer back to your helper method, which is then output by the<%=, causing your entire page to be duplicated.Normally, you should not need to use
concatmuch if at all (I’ve never come across a situation where I needed it). In your helper, you can just do this:And then use
<%= test %>in your view.