开发者

How to create N spans with no spaces in between in Rails 3?

开发者 https://www.devze.com 2023-03-09 15:43 出处:网络
To crea开发者_JAVA百科te n spans next to each other I do: <% n.times do |i| %> <%= content_tag(:span, i + 1) %>

To crea开发者_JAVA百科te n spans 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 spans.

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 spans 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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消