jackster the jackle
2010-Jan-21 21:48 UTC
Iterating through an Instance Variable in my View
I have an instance variable called @ls which is an array that contains a list of files in a directory. When I call this instance variable in my view like this: <%= @ls %> It shows the entire list of files in the array. Since I need to edit what is displayed for each file name in the array, I want to iterate through it in my view. When I put this code in my view: <% @ls.each do |a| %> a.puts <% end %> ... it only prints out the letter "a" as many times as the size of the array. Can anyone help me out with correcting this issue? thanks jackster -- 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.
Rob Biedenharn
2010-Jan-21 22:25 UTC
Re: Iterating through an Instance Variable in my View
On Jan 21, 2010, at 4:48 PM, jackster the jackle wrote:> I have an instance variable called @ls which is an array that > contains a > list of files in a directory. > > When I call this instance variable in my view like this: > > <%= @ls %> > > It shows the entire list of files in the array.Because there''s an implicit to_s on the contents of <%= %>> Since I need to edit what is displayed for each file name in the > array, > I want to iterate through it in my view. > > When I put this code in my view: > <% @ls.each do |a| %> > a.puts > <% end %> > > ... it only prints out the letter "a" as many times as the size of the > array. > > Can anyone help me out with correcting this issue? > > thanks > > jacksterYou probably want them in some sort of list, right? <ul> <% @ls.each do |a| %> <li> <%= a %> </li> <% end %> </ul> Replace the ul/li markup with whatever makes sense for your results. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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.