Joshua Muheim
2006-Sep-03 11:02 UTC
Undefined method "xxx" of a model when calling a helper
Hi all I''m creating a shop page. I have a cart model that looks like the following: class Cart attr_reader :line_items def get_line_item id @line_items.each do |l| return l if l.id == id end end def initialize @line_items = [] end def empty! initialize end def empty? return true if @line_items.empty? false end end And I have some helper methods in application_helper.rb: module ApplicationHelper def find_cart session[:cart] ||= Cart.new end def empty_cart find_cart.empty! end def cart_empty? return true if find_cart.empty? end end Then I want to display a partial form within the layout application.rhtml but only if the cart is not empty: <%= render(:partial => ''warenkorb/warenkorb'', :locals => {:cart => find_cart}) unless cart_empty? %> But this displays me the following error: undefined method `empty?'' for #<Cart:0x230020c @line_items=[]> Where does this error come from? I just don''t get it, the Cart object is instantiated when calling find_cart, and the Cart class definitely does have a method called "empty?"! Thanks for help, Joshua -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Sep-03 12:26 UTC
Re: Undefined method "xxx" of a model when calling a helper
Hi -- On Sun, 3 Sep 2006, Joshua Muheim wrote:> > Hi all > > I''m creating a shop page. I have a cart model that looks like the > following: > > class Cart > attr_reader :line_items > > def get_line_item id > @line_items.each do |l| > return l if l.id == id > end > enddef get_line_item(id) @line_items.find {|l| l.id == id } end> def initialize > @line_items = [] > end > > def empty! > initialize > end > > def empty? > return true if @line_items.empty? > false > enddef empty? @line_items.empty? end> end > > And I have some helper methods in application_helper.rb: > > module ApplicationHelper > def find_cart > session[:cart] ||= Cart.newIt seems a little odd to me to be manipulating the session data in a view (or view helper). You might want to find a way to juggle things so that this is back in the controller.> end > > def empty_cart > find_cart.empty! > end > > def cart_empty? > return true if find_cart.empty? > enddef cart_empty? find_cart.empty? end> end > > Then I want to display a partial form within the layout > application.rhtml but only if the cart is not empty: > > <%= render(:partial => ''warenkorb/warenkorb'', :locals => {:cart => > find_cart}) unless cart_empty? %> > > But this displays me the following error: > > undefined method `empty?'' for #<Cart:0x230020c @line_items=[]> > > Where does this error come from? I just don''t get it, the Cart object is > instantiated when calling find_cart, and the Cart class definitely does > have a method called "empty?"!I''m afraid I can''t duplicate your error. I''ve tried to make up for it with a few code hints :-) (see above) Could the problem have something to do with caching and then changing the model file? David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne
2006-Sep-03 15:13 UTC
Re: Undefined method "xxx" of a model when calling a helper
Joshua Muheim wrote:> class Cart > ... > def empty? > return true if @line_items.empty? > false > end > end > > And I have some helper methods in application_helper.rb: > > module ApplicationHelper > def cart_empty? > return true if find_cart.empty? > end > endNot sure why you''re getting that error. Are you sure you dont have a nother file redefing the cart class somewhere? It''s coming from "find_cart.empty?" in your helper method. Also, I''m not sure if this will help, but those methods could a whole lot simpler. in your empty? method, @line_items.empty? already returns true or false so it can be greatly simplified to: def empty? @line_items.empty? end And the same with your cart_empty? helper. But why use a helper for that when you can ask the cart directly if its empty? It''s less code and easier to troubleshoot. <%= render :partial ... unless find_cart.empty? %> Lastly, do all the other method on your cart object work ok? -- 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 -~----------~----~----~----~------~----~------~--~---