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
Try putting it in your app/helpers/application_helper.rb file...
-philip
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
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/.
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
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
--
http://www.robsanheim.com
http://www.seekingalpha.com
http://www.ajaxian.com
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.
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
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
> 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