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

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

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:

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@gmail.com wrote:

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

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

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 placed the method in ApplicationHelper?

Paste all the relevant code from app helper and from the rhtml file where you are calling it.

- Rob

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:

comicgeekspeak@gmail.com 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? %>

You also need the full name, with the question mark, where you call it from the view.

- rob

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