Trying to understand the difference between similar iterator


What’s the difference between these two?
Trying to display the addresses of the students that has “has_many”
relations with addresses

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

This code worked:

<% @student.addresses.each do |address|%> <%= address.addr1 %>
<% end %>

Any comments appreciated.
Silvy Mathews


You shouldn’t need the <% end %> because you are passing in a block.
This should work as well.

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

<% } %>


removed_email_address@domain.invalid wrote:

<% @student.addresses.each do |address|%> removed_email_address@domain.invalid

Eric G.


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:

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