I know, I know, they''re not allowed to. But here''s my problem. I''ve got a really simple model with two fields, name and price, which I want to show in a form select drop-down. "collection_select" takes a ''text_method'' parameter, which is the method in the model that gets its value placed in the <option> tag. Because I want to show both name and price, I had to add an extra method in my model- def description "#{name} - #{number_to_currency(price)}" end But this fails, because now I''m trying to reference number_to_currency (a helper method) from my model. How can I get around this? -- 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 -~----------~----~----~----~------~----~------~--~---
Hi,> But this fails, because now I''m trying to reference number_to_currency > (a helper method) from my model. How can I get around this? >Well.. as you said it''s not the best idea since you are somehow breaking the rules of MVC by using methods which are supposed to be only used by the views. That said, a helper is just a ruby module, meaning you can use it in any class you want if you just include it. The only problem you can find is that some helper methods will assume they are running in a normal controller-view cycle (as they should), and they will try to use some of the magic variables that get instantiated automatically behind the scenes (such as the request, for example). In case the helper you want to use doesn''t try to use any of those variables, you should be able to just include the helper module in your model and use all the included methods directly. regards, javier ramírez --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Can''t you just add the number_to_currency in the view? Keep things simple. On Feb 5, 12:02 pm, javier ramirez <jrami...-7iWCczGtl7hBDgjK7y7TUQ@public.gmane.org> wrote:> Hi, > > > But this fails, because now I''m trying to reference number_to_currency > > (a helper method) from my model. How can I get around this? > > Well.. as you said it''s not the best idea since you are somehow breaking > the rules of MVC by using methods which are supposed to be only used by > the views. > > That said, a helper is just a ruby module, meaning you can use it in any > class you want if you just include it. The only problem you can find is > that some helper methods will assume they are running in a normal > controller-view cycle (as they should), and they will try to use some of > the magic variables that get instantiated automatically behind the > scenes (such as the request, for example). > > In case the helper you want to use doesn''t try to use any of those > variables, you should be able to just include the helper module in your > model and use all the included methods directly. > > regards, > > javier ramírez--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Agreed, I''d rather not break MVC, which is why I''m a little frustrated with collection_select for referencing the *model* for what should be displayed in the select box... *grumble* In any case, thanks very much, your solution works. Liam javier ramirez wrote:> Hi, >> But this fails, because now I''m trying to reference number_to_currency >> (a helper method) from my model. How can I get around this? >> > > Well.. as you said it''s not the best idea since you are somehow breaking > the rules of MVC by using methods which are supposed to be only used by > the views. > > That said, a helper is just a ruby module, meaning you can use it in any > class you want if you just include it. The only problem you can find is > that some helper methods will assume they are running in a normal > controller-view cycle (as they should), and they will try to use some of > the magic variables that get instantiated automatically behind the > scenes (such as the request, for example). > > In case the helper you want to use doesn''t try to use any of those > variables, you should be able to just include the helper module in your > model and use all the included methods directly. > > regards, > > javier ram�rez-- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you can come up with a way to integrate that with collection_select, I''m all ears. ;) But collection_select seems to reference the model directly for display, which kind of breaks MVC. Melvin Ram wrote:> Can''t you just add the number_to_currency in the view? Keep things > simple.-- 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 -~----------~----~----~----~------~----~------~--~---
Go crazy and add number_to_currency to FixNum. Tehe. Probably dangerous, but still much fun! -Ryan On Feb 5, 2:11 pm, Liam Morley <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> If you can come up with a way to integrate that with collection_select, > I''m all ears. ;) But collection_select seems to reference the model > directly for display, which kind of breaks MVC. > > Melvin Ram wrote: > > Can''t you just add the number_to_currency in the view? Keep things > > simple. > > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- find_by_sql without a model? how to do this?
- number_to_currency() helper == non DRY == could be improved
- Book released "Aptana RadRails: An IDE for Rails Development"
- Cannot use view helpers in RJS helpers
- How do I reference eagerly loaded Models in the View?