where do custom methods go? i am trying to write a few methods but when i call them, no matter where i put them, rails says they are undefined. help! thanks- jason _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Do you mean custom methods on your models, or just helper methods? If you want to add some helper methods that can be accessed by your controller or model. place them in the relevent controller/<modelname>_helper.rb class, or in application_helper.rb if you want everyone to access them. sam On 5/4/05, jason lynes :: senyl.com <jason-9ZYj/DJVokUAvxtiuMwx3w@public.gmane.org> wrote:> where do custom methods go? i am trying to write a few methods but when i > call them, no matter where i put them, rails says they are undefined. help! > thanks- > > jason > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- sam http://www.magpiebrain.com/
For something you need in all your models, make a file and stick it in lib. Put your functions in a module. Require it in config/environment.rb, then open up ActiveRecord::Base and include the module. Alternatevely you could just include the module directly in all of your models, but this is 1-N and could be a maintenence pain. For a method you need in all controllers, put it in app/controllers/application.rb in the ApplicationController class. For a method you need in all views, put it in app/helpers/application_helper.rb. For a method you need in all controllers and views, put it in app/controllers/application.rb and mark it with the helper_method macro. For a helper method that is relevant to only one view, put it in app/helpers/viewname_helper.rb For a helper-type method that is needed by only one controller, put it in the controller and make it non-public (If you need this, you could be inadvertently putting model code in your controller. I rarely see this case) Anything you put in lib/ can be required in config/environment.rb directly and you''ll have access to it. I recommend wrapping your functions in modules to keep your namespace clean. Brian On 5/4/05, jason lynes :: senyl.com <jason-9ZYj/DJVokUAvxtiuMwx3w@public.gmane.org> wrote:> where do custom methods go? i am trying to write a few methods but when i > call them, no matter where i put them, rails says they are undefined. help! > thanks- > > jason > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Can I suggest that you put that in the Wiki? It seems like to good of a summary to get lost in the email archives. Thanks! Mike On 5/4/05, Brian L. <zorander-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > For something you need in all your models, make a file and stick it in > lib. Put your functions in a module. Require it in > config/environment.rb, then open up ActiveRecord::Base and include the > module. Alternatevely you could just include the module directly in > all of your models, but this is 1-N and could be a maintenence pain. > > For a method you need in all controllers, put it in > app/controllers/application.rb in the ApplicationController class. > > For a method you need in all views, put it in > app/helpers/application_helper.rb. > > For a method you need in all controllers and views, put it in > app/controllers/application.rb and mark it with the helper_method > macro. > > For a helper method that is relevant to only one view, put it in > app/helpers/viewname_helper.rb > > For a helper-type method that is needed by only one controller, put it > in the controller and make it non-public (If you need this, you could > be inadvertently putting model code in your controller. I rarely see > this case) > > Anything you put in lib/ can be required in config/environment.rb > directly and you''ll have access to it. I recommend wrapping your > functions in modules to keep your namespace clean. > > Brian > > > On 5/4/05, jason lynes :: senyl.com <http://senyl.com> <jason-9ZYj/DJVokUAvxtiuMwx3w@public.gmane.org> > wrote: > > where do custom methods go? i am trying to write a few methods but when > i > > call them, no matter where i put them, rails says they are undefined. > help! > > thanks- > > > > jason > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > -- > The years ahead pick up their dark bags. > They move closer. There''s a slight rise in the silence > > then nothing. > -- > (If you''re receiving this in response to mail sent to > bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, > but mail will be forwarded here indefinitely) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
While browsing the Wiki this evening, I fortuitously came across the empty topic "Understanding Where To Put Custom Methods" (http://wiki.rubyonrails.com/rails/show/UnderstandingWhereToPutCustomMethods), so I took the liberty of just adding the text. I hope you don''t mind! On 5/4/05, Brian L. <zorander-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> For something you need in all your models, make a file and stick it in > lib. Put your functions in a module. Require it in > config/environment.rb, then open up ActiveRecord::Base and include the > module. Alternatevely you could just include the module directly in > all of your models, but this is 1-N and could be a maintenence pain. > > For a method you need in all controllers, put it in > app/controllers/application.rb in the ApplicationController class. > > For a method you need in all views, put it in app/helpers/application_helper.rb. > > For a method you need in all controllers and views, put it in > app/controllers/application.rb and mark it with the helper_method > macro. > > For a helper method that is relevant to only one view, put it in > app/helpers/viewname_helper.rb > > For a helper-type method that is needed by only one controller, put it > in the controller and make it non-public (If you need this, you could > be inadvertently putting model code in your controller. I rarely see > this case) > > Anything you put in lib/ can be required in config/environment.rb > directly and you''ll have access to it. I recommend wrapping your > functions in modules to keep your namespace clean. > > Brian > > > On 5/4/05, jason lynes :: senyl.com <jason-9ZYj/DJVokUAvxtiuMwx3w@public.gmane.org> wrote: > > where do custom methods go? i am trying to write a few methods but when i > > call them, no matter where i put them, rails says they are undefined. help! > > thanks- > > > > jason > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > -- > The years ahead pick up their dark bags. > They move closer. There''s a slight rise in the silence > > then nothing. > -- > (If you''re receiving this in response to mail sent to > bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, > but mail will be forwarded here indefinitely) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Mike Payson wrote:> While browsing the Wiki this evening, I fortuitously came across the > empty topic "Understanding Where To Put Custom Methods" > (http://wiki.rubyonrails.com/rails/show/UnderstandingWhereToPutCustomMethods), > so I took the liberty of just adding the text. I hope you don''t mind!Doh! I added the same content to a different page [1] just the other day. [1] http://wiki.rubyonrails.com/rails/show/UnderstandingWhatMethodsGoesWhere Double-doh for my leaving the (extra)empty topic there to mislead others. Do you think one title is a better fit than the other? I''m not crazy about having the word ''custom'' in there, because it''s not as if witting a method is a ''custom'' job(it''s par for the course). Maybe UndestandingWhereToPutMethods ? -- Lee
Lee O''Mara wrote:> Mike Payson wrote: > >> While browsing the Wiki this evening, I fortuitously came across the >> empty topic "Understanding Where To Put Custom Methods" >> (http://wiki.rubyonrails.com/rails/show/UnderstandingWhereToPutCustomMethods), >> >> so I took the liberty of just adding the text. I hope you don''t mind! > > > Doh! I added the same content to a different page [1] just the other day. > > [1] > http://wiki.rubyonrails.com/rails/show/UnderstandingWhatMethodsGoesWhere > > Double-doh for my leaving the (extra)empty topic there to mislead others. > > Do you think one title is a better fit than the other? I''m not crazy > about having the word ''custom'' in there, because it''s not as if witting > a method is a ''custom'' job(it''s par for the course). Maybe > UndestandingWhereToPutMethods ?Just to follow up my own post. I''ve consolidated the two pages[1][2] and the extra link from the understanding Rails page[3] has been removed. [1] http://wiki.rubyonrails.com/rails/show/UnderstandingWhatMethodsGoesWhere [2] http://wiki.rubyonrails.com/rails/show/UnderstandingWhereToPutCustomMethods [3] http://wiki.rubyonrails.com/rails/show/UnderstandingRails -- Lee