# Blocks in views

**URL:** <https://www.ruby-forum.com/t/blocks-in-views/76435>\
**Category:** Rails\
**Created:** [November 18, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435 "2006-11-18T20:42:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/1 "2006-11-18T20:42:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/2 "2006-11-18T20:57:22Z")

</div>

Hi, it should be

| item | instead of | @item |

Also, I would recommend reading the section of AWDWR on using partials  
to render collections.

Peace,

-Conrad

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/3 "2006-11-18T20:55:22Z")

</div>

Hi –

On Sat, 18 Nov 2006, zerohalo wrote:

> \<% 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’s happening is this:

\<%= … %\> interpolates the string representation of whatever “…”  
evaluates to. In this case, “…” is a call to @items.each. The each  
method returns its receiver. So, in effect, what you’re doing is:

\<%= @items.to\_s %\>

If you do the “for @item” one, or this:

\<% @items each do |@item| %\>  
\<%= render :partial =\> “itemrow” %\>  
\<% end %\>

then you’re interpolating the string representation of the rendering  
of the partial, zero or more times in succession. You could also do:

\<%= @items.map {|@item| render :partial =\> “itemrow” } %\>

which would give you an array of all the rendered results. It’s a  
little less straightforward, though, and depends on representing an  
array as a string, which is an unnecessary extra step. Mainly I  
mention it to illustrate the principle at work, which the difference  
between each and map shows you.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/4 "2006-11-18T21:01:23Z")

</div>

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 P. wrote:

> \<%= 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?

–  
View this message in context:  
[http://www.nabble.com/-Rails--blocks-in-views-tf2661201.html#a7422902](http://www.nabble.com/-Rails--blocks-in-views-tf2661201.html#a7422902)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/5 "2006-11-18T21:09:23Z")

</div>

Hi –

On Sat, 18 Nov 2006, Conrad T. wrote:

> > 
> 
> | item | instead of | @item |  
> Actually you can use instance variables as block params. Try this in  
> irb:

[1,2,3].each {|@x| p @x }

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/6 "2006-11-18T21:15:24Z")

</div>

You _can_ – the syntax is parseable – but that’s not usually what you  
meant. If you needed an instance variable for a method call, I guess  
this  
would make sense, but it still seems strange and an unintentional use of  
legal syntax.

dblack wrote:

> Actually you can use instance variables as block params. Try this in  
> irb:
> 
> [1,2,3].each {|@x| p @x }

–  
View this message in context:  
[http://www.nabble.com/-Rails--blocks-in-views-tf2661201.html#a7423050](http://www.nabble.com/-Rails--blocks-in-views-tf2661201.html#a7423050)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/7 "2006-11-18T21:19:25Z")

</div>

Hi –

On Sat, 18 Nov 2006, s.ross wrote:

> would make sense, but it still seems strange and an unintentional use of  
> legal syntax.

I’ve done it sometimes when I want to do something like:

\<% @items.each do |@item| %\>  
\<%= text\_field “item[]”, “name” %\>  
\<% end %\>

It could also come in handy with rendering a partial, as it could save  
you the trouble of doing a :locals thing.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 19, 2006, 5:36am UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/8 "2006-11-19T05:36:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [November 18, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/blocks-in-views/76435/9 "2006-11-18T21:11:24Z")

</div>

Hi –

On Sat, 18 Nov 2006, s.ross wrote:

> > \<% 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

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
