Thursday, July 1, 2010

Ruby on Rails - access user defined lib functions in /lib/login_system.rb from app/views/layouts/_menu.rhtml

Programmer Question

I have defined a function called "is_logged_in?" in a user defined library stored in the /lib directory, however when I try to use it in one of my views (in this case a _menu.html.erb view) I get a "undefined method `is_logged_in?' for #" error. I had assumed that if the method was available within the /lib directory then it would be accessible through the application?



my login_system.rb file is as follows: -
module LoginSystem
protected



def is_logged_in?
@logged_in_user = User.find(session[:user]) if session[:user]
end



def logged_in_user
return @logged_in_user if is_logged_in?
end



def logged_in_user=(user)
if !user.nil?
session[:user] = user.id
@logged_in_user = user
end
end



def self.included(base)
base.send :helper_method, :is_logged_in, :logged_in_user
end
end



and my _menu.html.erb file is as follows: -










  • Logged in as:

  • 'account', :action => 'logout'}, :method => :post%>


  • 'users', :action => 'new' %>

  • 'account', :action => 'login' %>




Can anyone point where I've gone wrong?



Bernard



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails