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?
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
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
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
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.
[email protected] 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
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
On 11/7/06, [email protected] [email protected] wrote:
if cookies[:userid] != ""
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
–
On 11/7/06, [email protected] [email protected] 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.
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.
[email protected] wrote:
<% 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 also need the full name, with the question mark, where you call it
from the view.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs