I am sorry for the newbie question, but I am really stuck and I can''t work it out. :( Have 2 models. category and product (has_many and belongs_to). My routes.rb: map.resources :categories do |category| category.resources :products end index in category controller: def index @category = Category.find(params[:id]) @products = @category.products end and then in my index.rhtml: <% for product in @products %> <%= product.title %>.... <% end %> it will end up with this error: undefined method `title'' for #<Class:0x54eb998> I am sure the title is column name in my table. I can create a new product, edit it, delete it, but I am not able to show the data properly. Can anyone help me? Thanks in advance. Peter -- 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 -~----------~----~----~----~------~----~------~--~---
On Feb 18, 2009, at 5:23 PM, Peter Killik wrote:> I am sorry for the newbie question, but I am really stuck and I can''t > work it out. :( Have 2 models. category and product (has_many and > belongs_to). > > My routes.rb: > map.resources :categories do |category| > category.resources :products > end > > index in category controller: > def index > @category = Category.find(params[:id]) > @products = @category.products > end > > and then in my index.rhtml: > > <% for product in @products %> > <%= product.title %>.... > <% end %>Read: http://blog.grayproductions.net/articles/the_evils_of_the_for_loop and change to: <% @products.each do |product| %>> it will end up with this error: > undefined method `title'' for #<Class:0x54eb998> > > I am sure the title is column name in my table. I can create a new > product, edit it, delete it, but I am not able to show the data > properly. > > Can anyone help me? Thanks in advance. > PeterYou''re saying/showing CategoryController, but do you mean ProductsController? Since the undefined ''title'' is attributed to #<Class:...> and not #<Product:...> do you have a naked title somewhere that''s being interpreted as a method call on the anonymous view class? Is that the correct line from the view? -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-/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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> Read: > http://blog.grayproductions.net/articles/the_evils_of_the_for_loop > and change to: > > <% @products.each do |product| %>Thx Rob, I changed the for_loop and it works. THnaks. Peter -- 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 -~----------~----~----~----~------~----~------~--~---