Silvy@funmail.com
2006-Jan-28 00:10 UTC
[Rails] Trying to understand the difference between similariterators
No I have tried that. It gives me just this output # -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Pat Maddox Sent: Friday, January 27, 2006 2:48 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Trying to understand the difference between similariterators 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 On 1/27/06, Silvy@funmail.com <Silvy@funmail.com> wrote:> 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Alex Young
2006-Jan-28 18:01 UTC
[Rails] Trying to understand the difference between similariterators
Silvy@funmail.com wrote:> No I have tried that. It gives me just this output # >That''s because the <%= %> construct outputs the return value of the expression inside it. The .each method actually returns the object it''s called on - which in this case would be @student.addresses, which isn''t a string... Try: <td><%= @student.addresses.inject{|s, address| s + "Address: #{address.addr1}<br />"} %></td> -- Alex
Alex Young
2006-Jan-28 18:06 UTC
[Rails] Trying to understand the difference between similariterators
Alex Young wrote:> Silvy@funmail.com wrote: >> No I have tried that. It gives me just this output # > That''s because the <%= %> construct outputs the return value of the > expression inside it. The .each method actually returns the object it''s > called on - which in this case would be @student.addresses, which isn''t > a string... Try: > > <td><%= @student.addresses.inject{|s, address| s + "Address: > #{address.addr1}<br />"} %></td> >Oops... That should probably be <td><%= @student.addresses.inject(''''){|s, address| s + "Address: #{address.addr1}<br />"} %></td> -- Alex