Newbie question -- I thought that helpers were available in both views and controllers. The methods I''m putting in application_helper.rb don''t seem to be available in the controllers. Am I missing something? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The helper methods are only available to the views. Method heirarchy goes a bit like this: Model Controller Helper View With all the elements being able to access any method from any element above it. Don''t put model code in the view because it will slow your application down. If you''re doing something like a Model.find, stick it in your controller. If you''re defining a new method for an object of Model, stick it in the model file. Helpers are methods you can call from the view to dry up your views. For example if you wanted to duplicate the e''s in a sentence such as: "The quick brown fox jumped over the lazy dog", put this in your view: <%= duplicate_e("The quick brown fox jumped over the lazy dog") %> and this in your helper: def duplicate_e(string) string.gsub("e","ee") end On Jan 10, 2008 9:26 AM, Decker <hpinsley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Newbie question -- I thought that helpers were available in both views > and controllers. The methods I''m putting in application_helper.rb > don''t seem to be available in the controllers. Am I missing something? > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan, You''re the best! Thanks! On Jan 9, 6:04 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The helper methods are only available to the views. > > Method heirarchy goes a bit like this: > > Model > Controller > Helper > View > > With all the elements being able to access any method from any element above > it. > > Don''t put model code in the view because it will slow your application down. > If you''re doing something like a Model.find, stick it in your controller. If > you''re defining a new method for an object of Model, stick it in the model > file. Helpers are methods you can call from the view to dry up your views. > For example if you wanted to duplicate the e''s in a sentence such as: "The > quick brown fox jumped over the lazy dog", put this in your view: > > <%= duplicate_e("The quick brown fox jumped over the lazy dog") %> > > and this in your helper: > > def duplicate_e(string) > string.gsub("e","ee") > end > > On Jan 10, 2008 9:26 AM, Decker <hpins...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Newbie question -- I thought that helpers were available in both views > > and controllers. The methods I''m putting in application_helper.rb > > don''t seem to be available in the controllers. Am I missing something? > > -- > Ryan Bigghttp://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2008-Jan-10 01:59 UTC
Re: Basic Question - From where are helpers available
On Jan 9, 2008, at 5:04 PM, Ryan Bigg wrote:> The helper methods are only available to the views. >But if you want, you can define the method in the controller and "export" it using helper_method() and then you can use it in the view. So in your controller, you could do something like helper_method :my_method def my_method end http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeff Pritchard
2008-Jan-10 03:54 UTC
Re: Basic Question - From where are helpers available
Phillip Koebbe wrote:> On Jan 9, 2008, at 5:04 PM, Ryan Bigg wrote: > >> The helper methods are only available to the views. >> > > But if you want, you can define the method in the controller and > "export" it using > > helper_method() > > and then you can use it in the view. > > So in your controller, you could do something like > > helper_method :my_method > > def my_method > > end > > http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers > > Peace, > PhillipHmmmn, what if you put a method in the "application.rb" controller and then used helper_method() on it? Would it then be available to all controllers and all views? thanks, jp -- 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 -~----------~----~----~----~------~----~------~--~---
If you put a method in the application.rb it should be available to all controllers that descend from ApplicationController, but referencing it in a view won''t work. On Jan 10, 2008 2:24 PM, Jeff Pritchard <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Phillip Koebbe wrote: > > On Jan 9, 2008, at 5:04 PM, Ryan Bigg wrote: > > > >> The helper methods are only available to the views. > >> > > > > But if you want, you can define the method in the controller and > > "export" it using > > > > helper_method() > > > > and then you can use it in the view. > > > > So in your controller, you could do something like > > > > helper_method :my_method > > > > def my_method > > > > end > > > > http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers > > > > Peace, > > Phillip > > > Hmmmn, what if you put a method in the "application.rb" controller and > then used helper_method() on it? Would it then be available to all > controllers and all views? > > thanks, > jp > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2008-Jan-10 04:24 UTC
Re: Basic Question - From where are helpers available
On Jan 9, 2008, at 9:54 PM, Jeff Pritchard wrote:> > Phillip Koebbe wrote: >> On Jan 9, 2008, at 5:04 PM, Ryan Bigg wrote: >> >>> The helper methods are only available to the views. >>> >> >> But if you want, you can define the method in the controller and >> "export" it using >> >> helper_method() >> >> and then you can use it in the view. >> >> So in your controller, you could do something like >> >> helper_method :my_method >> >> def my_method >> >> end >> >> http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers >> >> Peace, >> Phillip > > > Hmmmn, what if you put a method in the "application.rb" controller and > then used helper_method() on it? Would it then be available to all > controllers and all views? > > thanks, > jpI just put this in application.rb helper_method :say_hi def say_hi "hi" end and create a test.rhtml for an arbitrary controller that consisted of <%= say_hi %> and when I ran http://localhost:3000/arbitrary_controller/test, I saw "hi". So it seems that it does, in fact, work. At least with Rails 1.2.3. Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---