what is the proper way to loop through a list in a rails view (assuming that you need to check if it is empty or not) -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
my_list.each do | an_item | puts "#{an_item}" end If the list is empty nothing is printed. On Fri, 2008-02-22 at 00:38 +0100, Jimmy Palmer wrote:> what is the proper way to loop through a list in a rails view (assuming > that you need to check if it is empty or not)--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Hoehne wrote:> my_list.each do | an_item | > puts "#{an_item}" > end > > If the list is empty nothing is printed.should i return my lists from the model? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<%- @records.each do |record| -%> here you handle each record, doing what has to be done <%- end -%> If @records is an empty array or other Enumerable, then the inside of the loop will never be executed. If @records is nil, you''ll throw an error. If you just want to remove all empty elements in your array, before looping, you can just do this in your controller: @records.compact! This removes all nil elements. Then when you loop through it in the view, you only get real records. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Scenario 1: # # Find all the models on your index page. # def index @items = MyModel.find(:all) end index.html.erb: <ul> <% @items.each do | an_item | %> <li><%= an_item.some_value %></li> <% end %> </ul> Scenario 2: class MyModel < ActiveRecord::Base has_many :other_models end class OtherModel < ActiveRecord::Base belongs_to :my_model end # # Find all children for a given model using has_many # def show @the_parent = MyModel.find(params[:id]) end show.html.er: <p><%= @the_parent.some_value %></p> <% @the_parent.other_models.each do | a_child | %> <p><%= a_child.some_value %></p> <% end %> Scenario 3: class MyModel < ActiveRecord::Base acts_as_tree end # # Find all children in the heirarchy # def show @the_parent = MyModel.find(params[:id]) end show.html.erb: <p><%= @the_parent.some_value %></p> <% @the_parent.children.each do | child | %> <p><%= child.some_value %></p> <% end %> In all these scenarios if the list is empty (i.e. none were found, there are no children in a has_many relationship, or no children in a tree) then the "each" blocks print nothing. You don''t need to test to see if the list is empty (unless you want to explicitly print something like "No values" if the list is empty). Testing if the list is empty you could use the "size" method on the returned collection. Generally, if you are going to display a list of things (for example all the items from a query) you will process the list inside an erb/rhtml file. On Fri, 2008-02-22 at 00:46 +0100, Jimmy Palmer wrote:> Paul Hoehne wrote: > > my_list.each do | an_item | > > puts "#{an_item}" > > end > > > > If the list is empty nothing is printed. > > should i return my lists from the model?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brent Miller wrote:> <%- @records.each do |record| -%> > here you handle each record, doing what has to be done > <%- end -%> > > If @records is an empty array or other Enumerable, then the inside of > the loop will never be executed. If @records is nil, you''ll throw an > error. > > If you just want to remove all empty elements in your array, before > looping, you can just do this in your controller: > > @records.compact! > > This removes all nil elements. Then when you loop through it in the > view, you only get real records.so should I always do a nil check on @records before I loop through the records? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you use a find(:all, ...) method you will get back a collection of an empty collection. If you navigate a relationship you will get a collection or an empty collection. Generally, in rails, methods that return a collection will return an empty collection, not nil. If, however, you have code like: def some_action @thing = MyModel.find(params[:id]) if @thing.some_test @list_of_stuff = @thing.children end end Then you would need to test @list_of_stuff to see if it''s nil. But that''s probably bad form. On Fri, 2008-02-22 at 03:25 +0100, Jimmy Palmer wrote:> Brent Miller wrote: > > <%- @records.each do |record| -%> > > here you handle each record, doing what has to be done > > <%- end -%> > > > > If @records is an empty array or other Enumerable, then the inside of > > the loop will never be executed. If @records is nil, you''ll throw an > > error. > > > > If you just want to remove all empty elements in your array, before > > looping, you can just do this in your controller: > > > > @records.compact! > > > > This removes all nil elements. Then when you loop through it in the > > view, you only get real records. > > so should I always do a nil check on @records before I loop through the > records?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Hoehne wrote:> If you use a find(:all, ...) method you will get back a collection of an > empty collection. If you navigate a relationship you will get a > collection or an empty collection. Generally, in rails, methods that > return a collection will return an empty collection, not nil. > > If, however, you have code like: > > def some_action > @thing = MyModel.find(params[:id]) > if @thing.some_test > @list_of_stuff = @thing.children > end > end > > Then you would need to test @list_of_stuff to see if it''s nil. But > that''s probably bad form.thanks guys. I found that my problem was that i was using find_all_by incorrectly and it was returning nil. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---