Does anyone know where i can put a helper method so it can be accessed from ANYWHERE? ie from a model, a controller or a view? Currently i''ve got model helper methods in lib/model_extensions.rb, controller helper methods in application.rb and view helper methods in application_helper.rb. But there''s lots of things (usually text helpers) that i want to be available to anything. There must be a place for these, but does anyone know where? thanks max -- 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 max. I tried to understand you :D Because my english not enough. Anyway the helpers are in our helper directories as you know. We can use them in our views. But the important point, if you wanna write your own helper and you wanna use it all your views you must write your helper to application_helper.rp For example. i use this def ----------------- application_helper.rb----------------- def my_helper(number) number_to_currency(number) end and in my view ------------ list.rhtml --------- Price: <%= my_helper(book.price) %> that is it. my explorer show this: Price: --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alper Türker wrote: But the important point,> if you wanna write your own helper and you wanna use it > all your views you must write your helper to application_helper.rp >Hi Alper - i''m sorry, but you did misunderstand my question. I was asking if it''s possible to put a single helper method in a single place so that it can be used in all views, models *and* controllers. -- 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 -~----------~----~----~----~------~----~------~--~---
On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Does anyone know where i can put a helper method so it can be accessed > from ANYWHERE? ie from a model, a controller or a view? > > Currently i''ve got model helper methods in lib/model_extensions.rb, > controller helper methods in application.rb and view helper methods in > application_helper.rb. > But there''s lots of things (usually text helpers) that i want to be > available to anything. > > There must be a place for these, but does anyone know where?You can put stuff in lib/ and "require" it from config/environment.rb Typically you would define methods in a module that would be mixed in when needed using "include". --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> There must be a place for these, but does anyone know where? > You can put stuff in lib/ and "require" it from config/environment.rb > > Typically you would define methods in a module that would be mixed in > when needed using "include".Hmm, can''t get this to work... I made a file called general_helpers.rb and put it in my lib folder. It has this: module GeneralHelpers def hello "hello world!" end end then, in one of my views, i have include ''general_helpers'' <span><%= hello %></span> And this generates an "unknown method or attribute "hello"" error. (I already do something similar with a module called ModelExtensions, in lib/model_extensions.rb, which i require in my environment file. The methods in there can be used by models, but not views or controllers.) -- 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 -~----------~----~----~----~------~----~------~--~---
Jonathan Rochkind
2007-Sep-20 16:52 UTC
Re: Helper method for models, controllers and views?
There are probably several ways to do this. Me, I''d just make it a class method on the model (odds are, it''s really business logic that belongs to the model). class MyModule def self.whatever # whatever end end Somewhere else, call: MyModule.whatever() It won''t have access to instance data and methods with self. of course, but if you want it to be callable from model, view, and controller, then what instance data would it have access to anyway? Model and view/controller usually have completely distinct methods and accessors! If your model and view/controller are going to have some methods in common, and you want this ''helper'' to take advantage of them, then you''ve got to go the module include route others have said. Jonathan Max Williams wrote:> Does anyone know where i can put a helper method so it can be accessed > from ANYWHERE? ie from a model, a controller or a view? > > Currently i''ve got model helper methods in lib/model_extensions.rb, > controller helper methods in application.rb and view helper methods in > application_helper.rb. > But there''s lots of things (usually text helpers) that i want to be > available to anything. > > There must be a place for these, but does anyone know where? > > thanks > max-- 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 Sep 20, 2007, at 12:30 PM, Max Williams wrote:> Bob Showalter wrote: >> On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> There must be a place for these, but does anyone know where? >> You can put stuff in lib/ and "require" it from config/environment.rb >> >> Typically you would define methods in a module that would be mixed in >> when needed using "include". > > Hmm, can''t get this to work... > > I made a file called general_helpers.rb and put it in my lib > folder. It > has this: > > module GeneralHelpers > def hello > "hello world!" > end > end > > then, in one of my views, i have > > include ''general_helpers''include GeneralHelpers> > <span><%= hello %></span> > > And this generates an "unknown method or attribute "hello"" error. > > (I already do something similar with a module called > ModelExtensions, in > lib/model_extensions.rb, which i require in my environment file. The > methods in there can be used by models, but not views or controllers.)Under Rails, you don''t even have to require ''general_helpers'' as it will be autoloaded. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Rob Biedenharn wrote:> On Sep 20, 2007, at 12:30 PM, Max Williams wrote: >> I made a file called general_helpers.rb and put it in my lib >> >> include ''general_helpers'' > > include GeneralHelpers> Under Rails, you don''t even have to require ''general_helpers'' as it > will be autoloaded. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgThanks rob - this is still crashing with an "unknown method" problem though. I''m not doing anything in environment.rb, just saying, in my view page: include GeneralHelpers <span><%= hello %></span> -- 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 -~----------~----~----~----~------~----~------~--~---
Jonathan Rochkind
2007-Sep-20 17:12 UTC
Re: Helper method for models, controllers and views?
I''ve noticed that sometimes the stack trace stops at the .rhtml file, when the error was really in the method being called. Is there something in the ''hello'' method definition that could be raising the method not found exception? ruby-debug may be helpful for debugging. It works well with Rails. Max Williams wrote:> Rob Biedenharn wrote: >> On Sep 20, 2007, at 12:30 PM, Max Williams wrote: >>> I made a file called general_helpers.rb and put it in my lib >>> >>> include ''general_helpers'' >> >> include GeneralHelpers > >> Under Rails, you don''t even have to require ''general_helpers'' as it >> will be autoloaded. >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > Thanks rob - this is still crashing with an "unknown method" problem > though. I''m not doing anything in environment.rb, just saying, in my > view page: > > include GeneralHelpers > > <span><%= hello %></span>-- 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 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Bob Showalter wrote: > > On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> There must be a place for these, but does anyone know where? > > You can put stuff in lib/ and "require" it from config/environment.rb > > > > Typically you would define methods in a module that would be mixed in > > when needed using "include". > > Hmm, can''t get this to work... > > I made a file called general_helpers.rb and put it in my lib folder. It > has this: > > module GeneralHelpers > def hello > "hello world!" > end > end > > then, in one of my views, i have > > include ''general_helpers''wrong syntax. include takes a module name.> > <span><%= hello %></span>You''ll have to put include GeneralHelpers into class ApplicationHelper (in app/helpers/application_helper.rb) in order for those to be available in your views. --~--~---------~--~----~------------~-------~--~----~ 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 Sep 20, 2007, at 1:10 PM, Max Williams wrote:> Rob Biedenharn wrote: >> On Sep 20, 2007, at 12:30 PM, Max Williams wrote: >>> I made a file called general_helpers.rb and put it in my lib >>> >>> include ''general_helpers'' >> >> include GeneralHelpers > >> Under Rails, you don''t even have to require ''general_helpers'' as it >> will be autoloaded. >> >> -Rob > > Thanks rob - this is still crashing with an "unknown method" problem > though. I''m not doing anything in environment.rb, just saying, in my > view page: > > include GeneralHelpers > > <span><%= hello %></span>I think that you actually want to extend the view object with this method so do: extend GeneralHelpers and see if that fixes the problem. ''include'' is to give instance methods to a class/module while ''extend'' is to give methods to the object itself (frequently for class methods, but also on individual objects). -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Bob Showalter wrote:> On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> >> include ''general_helpers'' > > wrong syntax. include takes a module name. > >> >> <span><%= hello %></span> > > You''ll have to put > > include GeneralHelpers > > into class ApplicationHelper (in app/helpers/application_helper.rb) in > order for those to be available in your views.ahh, i didn''t realise it had to be in ApplicationHelper...ok, that works. Putting the same line in application.rb allows me to use the method in controllers as well. So, 2 out of 3 so far! :) However, i also put the same line in model_extensions, which gives methods that can be called by any model. But, when i try to call the method inside one of my model''s methods i get an "unknown method "hello"" error again. Thanks for your help - do you know why i''m not picking it up in my models? -- 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 -~----------~----~----~----~------~----~------~--~---
Max Williams wrote:> Bob Showalter wrote:> However, i also put the same line in model_extensions, which gives > methods that can be called by any model. But, when i try to call the > method inside one of my model''s methods i get an "unknown method > "hello"" error again. > > Thanks for your help - do you know why i''m not picking it up in my > models?Btw, this might be to do with the fact that ModelExtensions is itself a module - can you include a module in another module? -- 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 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Max Williams wrote: > > Bob Showalter wrote: > > > However, i also put the same line in model_extensions, which gives > > methods that can be called by any model. But, when i try to call the > > method inside one of my model''s methods i get an "unknown method > > "hello"" error again. > > > > Thanks for your help - do you know why i''m not picking it up in my > > models? > Btw, this might be to do with the fact that ModelExtensions is itself a > module - can you include a module in another module?Yes, you can include a module in another module. I''m not familiar with ModelExtensions, but something like this should work (put it in your general_helpers.rb) module ActiveRecord class Base include GeneralHelpers end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> > models? >> Btw, this might be to do with the fact that ModelExtensions is itself a >> module - can you include a module in another module? > > Yes, you can include a module in another module. > > I''m not familiar with ModelExtensions, but something like this should > work (put it in your general_helpers.rb) > > module ActiveRecord > class Base > include GeneralHelpers > end > endThat doesn''t seem to work for me...let me just run through what i have now as i may have got confused somewhere. I have a file in the lib folder called "general_helpers.rb" with module GeneralHelpers def hello "hello world!" end end module ActiveRecord class Base include GeneralHelpers end end And i have a method in my Movie model that looks like this: def hello_test return hello end when i call this method on an instance of type Movie (in a view) i get "undefined local variable or method `hello'' for #<Movie:0x44c9a38>" I tried restarting the server just in case, but it didn''t help... -- 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 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Bob Showalter wrote: > > On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> > models? > >> Btw, this might be to do with the fact that ModelExtensions is itself a > >> module - can you include a module in another module? > > > > Yes, you can include a module in another module. > > > > I''m not familiar with ModelExtensions, but something like this should > > work (put it in your general_helpers.rb) > > > > module ActiveRecord > > class Base > > include GeneralHelpers > > end > > end > > That doesn''t seem to work for me...let me just run through what i have > now as i may have got confused somewhere. > > I have a file in the lib folder called "general_helpers.rb" with > > module GeneralHelpers > def hello > "hello world!" > end > > end > > module ActiveRecord > class Base > include GeneralHelpers > end > end > > And i have a method in my Movie model that looks like this: > > > def hello_test > return hello > end > > when i call this method on an instance of type Movie (in a view) i get > > "undefined local variable or method `hello'' for #<Movie:0x44c9a38>" > > I tried restarting the server just in case, but it didn''t help...If you go into script/console and do this, it should work: >> GeneralHelpers GeneralHelpers >> Movie.new.hello "hello world!" The problem is that nothing is causing general_helpers.rb to be loaded. I forced it to load by referencing the module name as the first thing. You''ll need to go ahead and add to config/environment.rb (put it at the bottom) require ''general_helpers'' --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> > work (put it in your general_helpers.rb) >> I have a file in the lib folder called "general_helpers.rb" with >> include GeneralHelpers >> when i call this method on an instance of type Movie (in a view) i get >> >> "undefined local variable or method `hello'' for #<Movie:0x44c9a38>" >> >> I tried restarting the server just in case, but it didn''t help... > > If you go into script/console and do this, it should work: > > >> GeneralHelpers > GeneralHelpers > >> Movie.new.hello > "hello world!" > > The problem is that nothing is causing general_helpers.rb to be > loaded. I forced it to load by referencing the module name as the > first thing. > > You''ll need to go ahead and add to config/environment.rb (put it at the > bottom) > > require ''general_helpers''Thanks again Bob, but still no joy. Even the console doesn''t work - it seems to load the GeneralHelpers class ok, but give an "unrecognised method" error for hello. I must have done something wrong somewhere, i have no ide what though. -- 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 -~----------~----~----~----~------~----~------~--~---