comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-07 19:24 UTC
Functions in application.rb?
I have put a custom function in my application.rb file. However when I try to call this function from a view, I get an "undefined method" error. Are there any tricks to calling functions? Thanks, Bry --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> I have put a custom function in my application.rb file. > > However when I try to call this function from a view, I get an > "undefined method" error. > > Are there any tricks to calling functions?Try putting it in your app/helpers/application_helper.rb file... -philip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-07 19:49 UTC
Re: Functions in application.rb?
I tried that after I posted this, but I get the same result. Perhaps I should include my code so that you can see what I''m doing wrong. First, the function declaration: def isLoggedIn() if cookies[:userid] != "" return true else return false end end Second, calling the function from the view: <% if isLoggedIn() == true %> Is anything wrong with that? Bry Philip Hallstrom wrote:> > I have put a custom function in my application.rb file. > > > > However when I try to call this function from a view, I get an > > "undefined method" error. > > > > Are there any tricks to calling functions? > > Try putting it in your app/helpers/application_helper.rb file... > > -philip--~--~---------~--~----~------------~-------~--~----~ 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''re wanting to use your isLoggedIn method (not function :) in an rhtml file, it will need to be in a helper. By convention, rhtml has access to methods in the application_helper, or your controller''s helper file. Your application.rb file contains methods that your models and controllers can access. You need not to explicitly check if isLoggedIn == true. You can simply say: <% if isLoggedIn %> or <% if not isLoggedIn %> Also, just a nitpick, but a common idiom amongst Ruby developers is to use the mixed-case style only on Classes and Constants. Otherwise, use lowercase with underscores. comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I tried that after I posted this, but I get the same result. > > Perhaps I should include my code so that you can see what I''m doing > wrong. > > First, the function declaration: > > def isLoggedIn() > > if cookies[:userid] != "" > return true > else > return false > end > > end > > Second, calling the function from the view: > > <% if isLoggedIn() == true %> > > Is anything wrong with that? > > Bry-- 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 ComicGeekSpeak, I would imagine you put the code in the class definition of ApplicationController in which case you need an instance of ApplicationController to call the function or have it as a static function. If you copy it outside the class definition it should work but I am not sure thats the best thing to do. Regards, Sean Griffin comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I tried that after I posted this, but I get the same result. > > Perhaps I should include my code so that you can see what I''m doing > wrong. > > First, the function declaration: > > def isLoggedIn() > > if cookies[:userid] != "" > return true > else > return false > end > > end > > Second, calling the function from the view: > > <% if isLoggedIn() == true %> > > Is anything wrong with that? > > Bry > > > > Philip Hallstrom wrote: > > > I have put a custom function in my application.rb file. > > > > > > However when I try to call this function from a view, I get an > > > "undefined method" error. > > > > > > Are there any tricks to calling functions? > > > > Try putting it in your app/helpers/application_helper.rb file... > > > > -philip--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry cross posted with Wes post. His comment on the Method or Function is I think where the problem is. BTW I tested this and you can call functions in Application.rb provided its not in the class definition. However putting a function in the application_helper is a much better. SeanG --~--~---------~--~----~------------~-------~--~----~ 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 11/7/06, comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I tried that after I posted this, but I get the same result. > > Perhaps I should include my code so that you can see what I''m doing > wrong. > > First, the function declaration: > > def isLoggedIn() > > if cookies[:userid] != "" > return true > else > return false > end > > end > > Second, calling the function from the view: > > <% if isLoggedIn() == true %> > > Is anything wrong with that? > > Bry > > >This is what you want w/i ApplicationHelper (not the controller). Use lowercase, underscores, and question marks for methods that answer a true/false "question". These are the typical ruby standards followed by convention, you can break them but its better to follow them unless you have a good reason not to. def logged_in? cookies[:userid] != "" end You don''t need the if/else, and you don''t need explicit returns (tho it doesn''t hurt). You could probably also use the "blank?" method which handles nil as well and is a bit easier to read. If you need the method in BOTH controllers and the view, place it in the application controller and use helper_method see also: helper_method docs => http://rubyonrails.org/rails/classes/ActionController/Helpers/ClassMethods.html#M000139 blank? docs => http://caboo.se/doc/classes/Object.html#M003709 - Rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.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 -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-07 21:24 UTC
Re: Functions in application.rb?
I have tried all of the above tactics, yet I still receive the error: undefined local variable or method `logged_in'' I''m really confused. Bry --~--~---------~--~----~------------~-------~--~----~ 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 11/7/06, comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have tried all of the above tactics, yet I still receive the error: > > undefined local variable or method `logged_in'' > > I''m really confused. > > BryYou placed the method in ApplicationHelper? Paste all the relevant code from app helper and from the rhtml file where you are calling it. - Rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.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 -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-08 13:39 UTC
Re: Functions in application.rb?
Ok, here''s my code in application_helper.rb def logged_in? cookies[:userid] != "" end And here is where I call it in my standard.rhtml file which is used for every single page call: <% if logged_in %> Thanks, Bry Rob Sanheim wrote:> > You placed the method in ApplicationHelper? > > Paste all the relevant code from app helper and from the rhtml file > where you are calling it. > > - Rob > -- > http://www.robsanheim.com > http://www.seekingalpha.com > http://www.ajaxian.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 -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Ok, here''s my code in application_helper.rb > > def logged_in? > cookies[:userid] != "" > end > > > And here is where I call it in my standard.rhtml file which is used for > every single page call: > > <% if logged_in %>Is it a typo, or did you leave off the question mark? <% if logged_in? %> -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You also need the full name, with the question mark, where you call it from the view. - rob On 11/8/06, comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Ok, here''s my code in application_helper.rb > > def logged_in? > cookies[:userid] != "" > end > > > And here is where I call it in my standard.rhtml file which is used for > every single page call: > > <% if logged_in %> > > > Thanks, > > Bry > > Rob Sanheim wrote: > > > > > You placed the method in ApplicationHelper? > > > > Paste all the relevant code from app helper and from the rhtml file > > where you are calling it. > > > > - Rob--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-08 19:07 UTC
Re: Functions in application.rb?
I did leave off the question mark. I added it, and I still get the same error.> Is it a typo, or did you leave off the question mark? > > <% if logged_in? %>The only thing I can think of that might make my situation slightly different, is that I''m trying to do this in a standard.rhtml file in the layouts folder in the views folder. Bry --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---