Joe Ruby MUDCRAP-CE
2006-Oct-03 01:59 UTC
Creating your own global methods (like params, etc.)?
I put this in my app.rb: public def member if @member.nil? @member = Member.find_by_username(''joe'') else @member end end I figured I''d be able to use it in templates like so: text "Hi #{member.name}!" # text "Hi #{@helpers.member.name}!" # also tried this since I''m using markaby but that gives an "undefined method ''member''" error. However, if I put this in app.rb: before_filter :member it works. What''s up with that? At any rate, what''s the proper voodoo for creating your own "global" methods like params, request, etc.? Thanks, Joe -- 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 -~----------~----~----~----~------~----~------~--~---
Danny Burkes
2006-Oct-03 02:31 UTC
Re: Creating your own global methods (like params, etc.)?
> I put this in my app.rb: > > public > def member > if @member.nil? > @member = Member.find_by_username(''joe'') > else > @member > end > end > > I figured I''d be able to use it in templates like so: > > text "Hi #{member.name}!" > # text "Hi #{@helpers.member.name}!" # also tried this since I''m using > markaby > > but that gives an "undefined method ''member''" error. However, if I put > this in app.rb: >Views can only see helper methods- put "member" in app_helper.rb if you want it to be visible from views, or at least declare helper_method :member in app.rb> before_filter :member > > it works. What''s up with that? >No problem here- before_filter belongs to ActionController, and things in app.rb are visible from there.> At any rate, what''s the proper voodoo for creating your own "global" > methods like params, request, etc.? >You''ve got the idea- just need some visibility tweaks. BTW, congrats on the MUDCRAP certification...I''m planning on starting on online study course for MUDCRAP soon. PayPal will be gleefully accepted. - Danny -- 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 -~----------~----~----~----~------~----~------~--~---
Joe Ruby MUDCRAP-CE
2006-Oct-03 02:42 UTC
Re: Creating your own global methods (like params, etc.)?
Danny Burkes wrote:>> I put this in my app.rb: >> >> public >> def member >> if @member.nil? >> @member = Member.find_by_username(''joe'') >> else >> @member >> end >> end >> >> I figured I''d be able to use it in templates like so: >> >> text "Hi #{member.name}!" >> # text "Hi #{@helpers.member.name}!" # also tried this since I''m using >> markaby >> >> but that gives an "undefined method ''member''" error. However, if I put >> this in app.rb: >> > > Views can only see helper methods- put "member" in app_helper.rb if you > want it to be visible from views, or at least declare > > helper_method :member > > in app.rb > >> before_filter :member >> >> it works. What''s up with that? >> > > No problem here- before_filter belongs to ActionController, and things > in app.rb are visible from there.But the member method set @member -- I don''t know why ''member'' would be visible after the before filter runs. Unless it''s doing something wacky like interpolating member to @member.>> At any rate, what''s the proper voodoo for creating your own "global" >> methods like params, request, etc.? >> > > You''ve got the idea- just need some visibility tweaks. > > BTW, congrats on the MUDCRAP certification...I''m planning on > starting on online study course for MUDCRAP soon. PayPal will be > gleefully accepted.My life is vastly better since completing the MUDCRAP certification! Much better pay, higher IQ, better looks, heaps of accolades from peers! Joe> - Danny-- 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 -~----------~----~----~----~------~----~------~--~---