To crea开发者_JAVA百科te n
span
s next to each other I do:
<% n.times do |i| %>
<%= content_tag(:span, i + 1) %>
<% end %>
The problem is that there is a space between the created span
s.
So, I tried to create them in one line:
<% spans = "".html_safe %>
<% n.times do |i| %>
<% spans += content_tag(:span, i + 1) %>
<% end %>
<%= spans %>
Indeed, now the span
s are close to each other (no space in between).
However, I feel that this workaround is dirty.
How could achieve the same with a cleaner code ?
If you check the HTML source you can see it's not a space, but a line-break (which in HTML is displayed as a space). If you put everything in one line the spaces are gone:
<% n.times do |i| -%><%= content_tag(:span, i + 1) %><% end -%>
This gives me the following output:
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
try this
<% (0..n).map do |i|
content_tag(:span, i + 1) %>
end -%>
Or
<% n.times do |i| %>
<%= content_tag(:span, i + 1) %>
<% end %>
精彩评论