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

how to move to location in graphics mode

Programmer Question

What code do we use to move at a point on the screen in graphics mode?As in normal we use goto(x,y).



Find the answer here

How do I prevent site scraping?

Programmer Question

I have a fairly large music website with a large artist database. I've been noticing other music sites scraping our site's data (I enter dummy Artist names here and there and then do google searches for them).



How can I prevent screen scraping? Is it even possible?



Find the answer here

LinkWithin

Related Posts with Thumbnails