Hi, I''m trying to use number_to_currency in one of my models, but i''m getting the following error: undefined method `number_to_currency'' for #<Product:0x396d680> Obviously, my Product model can''t access number_to_currency -- is there something I need to do to make it available? Thanks so much for your assistance. -- Posted via http://www.ruby-forum.com/.
The "number" helpers are only available in your view. Cheers, M Damon Hill Project Manager IFWORLD, Inc. 479-582-5100 (p) 479-582-5599 (f) www.ifworld.com "..as we''re sung to sleep by philosophies that sing save the trees and kill the children...." -- while you were sleeping (casting crowns) -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of www-data Sent: Friday, August 18, 2006 11:13 AM To: rails@lists.rubyonrails.org Subject: [Rails] HELP!!!! - number_to_currency in model? Hi, I''m trying to use number_to_currency in one of my models, but i''m getting the following error: undefined method `number_to_currency'' for #<Product:0x396d680> Obviously, my Product model can''t access number_to_currency -- is there something I need to do to make it available? Thanks so much for your assistance. -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
As previously stated, those are view helpers. Models really shouldn''t have any formatting data in them at all... your view layer should do that for you. But explain why you want to do number_to_currency in the model (remember this renders a string) and there may be a way to solve your problem. You *can* get number_to_currency working in the model -- I''ve done it before but it''s not a good approach. On 8/18/06, www-data <www-data@andreas-s.net> wrote:> > Hi, > > I''m trying to use number_to_currency in one of my models, but i''m > getting the > following error: > > undefined method `number_to_currency'' for #<Product:0x396d680> > > Obviously, my Product model can''t access number_to_currency -- is there > something I need to do to make it available? > > Thanks so much for your assistance. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060818/96e44560/attachment-0001.html
Brian Hogan wrote:> As previously stated, those are view helpers. Models really shouldn''t > have > any formatting data in them at all... your view layer should do that for > you. > > But explain why you want to do number_to_currency in the model (remember > this renders a string) and there may be a way to solve your problem. You > *can* get number_to_currency working in the model -- I''ve done it before > but > it''s not a good approach.Thanks - I''m fairly new to rails, so maybe i''m going about this in the wrong way. I have a Product model with min_price and max_price properties. In many places throughout the site, I need to display a price range. I had this in my application_helper: def get_price_range(min,max) if min == max price_range_text = number_to_currency(max) else price_range_text = number_to_currency(min)+ '' - '' + number_to_currency(max) end end But I thought it would be nice to make this an accesible value on my Product model, so in my view I could just say <%=product.price_range%> as opposed to <%=get_price_range(product.min_price,product.max_price)%> So I tried adding this to my Product Model: def price_range if self.min_price == self.max_price price_range_text = number_to_currency(self.max_price) else price_range_text = number_to_currency(self.min_price)+ '' - '' + number_to_currency(self.max_price) end end But apparently I shouldn''t be doing this? -- Posted via http://www.ruby-forum.com/.
Your helper approach is the way to go actually. That''s what helpers are for. But make it a little easier to use by just explicitly returning the sring at the end of the mod def get_price_range(model) if model.min == model.max price_range_text = number_to_currency(model.max) else price_range_text = number_to_currency(model.min)+ '' - '' + number_to_currency(model.max) end price_range_text end Then your helper works like <p>Price range: <%=get_price_range @product %></p> Perfectly appropriate. Way to go. On 8/18/06, www-data <www-data@andreas-s.net> wrote:> > Brian Hogan wrote: > > As previously stated, those are view helpers. Models really shouldn''t > > have > > any formatting data in them at all... your view layer should do that for > > you. > > > > But explain why you want to do number_to_currency in the model (remember > > this renders a string) and there may be a way to solve your problem. You > > *can* get number_to_currency working in the model -- I''ve done it before > > but > > it''s not a good approach. > > Thanks - I''m fairly new to rails, so maybe i''m going about this in the > wrong way. > > I have a Product model with min_price and max_price properties. > > In many places throughout the site, I need to display a price range. I > had this in my application_helper: > > def get_price_range(min,max) > if min == max > price_range_text = number_to_currency(max) > else > price_range_text = number_to_currency(min)+ '' - '' + > number_to_currency(max) > end > end > > But I thought it would be nice to make this an accesible value on my > Product model, so in my view I could just say <%=product.price_range%> > as opposed to <%=get_price_range(product.min_price,product.max_price)%> > > So I tried adding this to my Product Model: > def price_range > if self.min_price == self.max_price > price_range_text = number_to_currency(self.max_price) > else > price_range_text = number_to_currency(self.min_price)+ '' - '' + > number_to_currency(self.max_price) > end > end > > But apparently I shouldn''t be doing this? > > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060818/631ea2e7/attachment.html