Functions in application.rb?

1 view
Skip to first unread message

comicge...@gmail.com

unread,
Nov 7, 2006, 2:24:36 PM11/7/06
to Ruby on Rails: Talk
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

Philip Hallstrom

unread,
Nov 7, 2006, 2:35:43 PM11/7/06
to Ruby on Rails: Talk

Try putting it in your app/helpers/application_helper.rb file...

-philip

comicge...@gmail.com

unread,
Nov 7, 2006, 2:49:57 PM11/7/06
to Ruby on Rails: Talk
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

Wes Rogers

unread,
Nov 7, 2006, 3:20:47 PM11/7/06
to rubyonra...@googlegroups.com
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.

comicge...@gmail.com 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/.

SeanG

unread,
Nov 7, 2006, 3:27:02 PM11/7/06
to Ruby on Rails: Talk
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

SeanG

unread,
Nov 7, 2006, 3:45:31 PM11/7/06
to Ruby on Rails: Talk
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

Rob Sanheim

unread,
Nov 7, 2006, 3:53:58 PM11/7/06
to rubyonra...@googlegroups.com
On 11/7/06, comicge...@gmail.com <comicge...@gmail.com> 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

comicge...@gmail.com

unread,
Nov 7, 2006, 4:24:39 PM11/7/06
to Ruby on Rails: Talk
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

Rob Sanheim

unread,
Nov 7, 2006, 7:25:30 PM11/7/06
to rubyonra...@googlegroups.com

You placed the method in ApplicationHelper?

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

comicge...@gmail.com

unread,
Nov 8, 2006, 8:39:37 AM11/8/06
to Ruby on Rails: Talk
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

Mark Reginald James

unread,
Nov 8, 2006, 9:02:22 AM11/8/06
to rubyonra...@googlegroups.com
comicge...@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? %>


--
We develop, watch us RoR, in numbers too big to ignore.

Rob Sanheim

unread,
Nov 8, 2006, 9:44:52 AM11/8/06
to rubyonra...@googlegroups.com
You also need the full name, with the question mark, where you call it
from the view.

- rob

comicge...@gmail.com

unread,
Nov 8, 2006, 2:07:46 PM11/8/06
to Ruby on Rails: Talk
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

Reply all
Reply to author
Forward
0 new messages