Forum: Ruby on Rails Trying to understand the difference between similar iterator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-01-28 00:39
(Received via mailing list)
What's the difference between these two?
Objective:
Trying to display the addresses of the students that has "has_many"
relations with addresses

<td><% @student.addresses.each {|address| "Address:
#{address.addr1}<br/>"} %> </td>   ****** did not work
<% end %>

This code worked:
<td><% @student.addresses.each do |address|%>
     <%= address.addr1 %><br/>
<% end %>

Any comments appreciated.
Thanks
Silvy Mathews
Eric G. (Guest)
on 2006-01-28 00:45
(Received via mailing list)
Hi,
You shouldn't need the <% end %> because you are passing in a block.
This should work as well.

<% @student.addresses.each {|address| %>
<%= address.addr1 %><br/>
<% } %>

Eric

removed_email_address@domain.invalid wrote:
> <td><% @student.addresses.each do |address|%>
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Eric G.
http://www.ericgoodwin.com
Pat M. (Guest)
on 2006-01-28 00:48
(Received via mailing list)
Well first of all, when you're iterating through a block, you don't
need to have an end.  In fact it should give you some kind of error.
Secondly, <% just evals a statement, <%= renders the output.  So your
loop should be:

<td><%= @student.addresses.each {|address| "Address:
#{address.addr1}<br/>"} %> </td>

Pat
This topic is locked and can not be replied to.