blocks in views

What do you mean by "doesn't work"? Error? No output? Only one line of output?

In any case, what <%= %> does is evaluate the Ruby inside it and concatenate the results onto the erb stream. The block you have specified returns, I believe, not a concatenation of the partials, but rather the last partial evaluated.

HTH

Daan Poron wrote:

Hi --

Daan Poron wrote:

This is more of an academic question, but I was wondering why the following doesn't work in a view:

<%= @items.each { |@item| render :partial => 'itemrow' } %>

Of course it can be done like this, which works:

<% for @item in @items %> <%= render :partial => 'itemrow' %> <% end %>

But I was curious as to why the first method doesn't work (at least it doesn't work for me). Is it not possible to use blocks in views?

What do you mean by "doesn't work"? Error? No output? Only one line of output?

I think the output will be a single hash-mark for each item in @items, representing a shortened string representation.

In any case, what <%= %> does is evaluate the Ruby inside it and concatenate the results onto the erb stream. The block you have specified returns, I believe, not a concatenation of the partials, but rather the last partial evaluated.

Actually it returns the original array, @items. The results of the calls to render are discarded.

David

Thanks, David, for the explanation. I didn't understand the difference between map and each.

And you're exactly right, the wrong output that I got with the block was simply the array @items -- the partial itself wasn't output even once.