CODE1: <% @posts.each do |p|%> <%=h p.name %> <% end %> this will show the name string like "Tom" in the web page. CODE2: <%=h @posts.each {|p| p.name} %> why this code return serial strings like #<Post:0x3a893e8> ? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 March 2010 16:33, Grick Zh <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> <%=h @posts.each {|p| p.name} %> > > why this code return serial strings like #<Post:0x3a893e8> ?Because you''re outputting the return value of the whole loop, not the output of each block. If you run that command in your console, you will see it return an array of Posts; and that''s what you''re trying to render. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If you want the names seperated by comma, try this: <%=h @posts.map{|p| p.name}.join('', '') %> On 18 mrt, 17:48, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 18 March 2010 16:33, Grick Zh <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > <%=h @posts.each {|p| p.name} %> > > > why this code return serial strings like #<Post:0x3a893e8> ? > > Because you''re outputting the return value of the whole loop, not the > output of each block. > If you run that command in your console, you will see it return an > array of Posts; and that''s what you''re trying to render.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Or more concisely (because it''s fun): <%=h @posts.map(&:name).to_sentence %> which will make it "Tom, Dick, and Harry". On Mar 18, 12:46 pm, tomkurt <tom.maeckelber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you want the names seperated by comma, try this: > > <%=h @posts.map{|p| p.name}.join('', '') %> > > On 18 mrt, 17:48, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On 18 March 2010 16:33, Grick Zh <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > <%=h @posts.each {|p| p.name} %> > > > > why this code return serial strings like #<Post:0x3a893e8> ? > > > Because you''re outputting the return value of the whole loop, not the > > output of each block. > > If you run that command in your console, you will see it return an > > array of Posts; and that''s what you''re trying to render.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote:> On 18 March 2010 16:33, Grick Zh <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> <%=h @posts.each {|p| p.name} %> >> >> why this code return serial strings like #<Post:0x3a893e8> ? > > Because you''re outputting the return value of the whole loop, not the > output of each block. > If you run that command in your console, you will see it return an > array of Posts; and that''s what you''re trying to render.I got it,thanks for your help :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Adam Stegman wrote:> Or more concisely (because it''s fun): > <%=h @posts.map(&:name).to_sentence %> > which will make it "Tom, Dick, and Harry".I''m newbie,can''t understand "(&:name)",but i know this code is skillful. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Grick Zh wrote:> Adam Stegman wrote: >> Or more concisely (because it''s fun): >> <%=h @posts.map(&:name).to_sentence %> >> which will make it "Tom, Dick, and Harry". >1) @posts.map This will return a new enumerable, replacing each element of @posts with whatever the block says 2) &:name Shortcut to indicate a block and a method. In this case, the method is :name (post.name) Other example: [1,2,3,4].inject(:+) # Sum every element of the array. Will of course fail if an element does not understand "+". 3) to_sentence We now have an array with names. "to_sentence" is, I believe, an activesupport method which joins the array into a string with commas, and adds "and " between the second to last and last item. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.