开发者

Rails prints string when each is used in html.erb

While i开发者_开发百科t could of course be done neater by putting the code into the controller or something, I can not image why the following is happening: assume that @some_table.some_text contains 5 lines. putting the following code in my html.erb file:

<% @some_table.some_text.lines.each do |cur_line| %>
    foo
<% end %>

results in 5 foos followed by all the lines in @some_table.some_text. I could imagine this would happen when using the <%= %> but not with <% %>. Obviously, I don't want the @some_table.some_text to be shown.

What am I doing wrong here?


That's just the way that the Ruby lines method works - it returns an Enumerable, which can't be looped through in the same way. For your purposes, try

<% @some_table.some_text.split(/\n/).each do |cur_line| %>

instead.

Alternatively convert the Enemerable into an array before calling each, using one of the methods, eg:

<% @some_table.some_text.lines.collect.each do |cur_line| %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜