views:

50

answers:

2

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:

<ul>
 <li><%= link_to 'Home', '/' %></li>
 <li><%= link_to 'Edit Page', pages_path %></li>

 <li><hr size = "1" width = "90%" aligh = "left" /></li>

 <% if is_logged_in? %>
  <li> Logged in as: <i><%= logged_in_user.username%> </i></li>
  <li><%= link_to 'Logout',{:controller => 'account', :action => 'logout'}, :method => :post%> </li>
 <% else %>
  <li><%= link_to 'Signup', :controller => 'users', :action => 'new' %> </li>
  <li><%= link_to 'Login', :controller => 'account', :action => 'login' %></li>
 <% end %>
</ul>

Can anyone point where I've gone wrong?

Bernard

+2  A: 

edit your ApplicationController:

class ApplicationController < ActionController::Base
  include 'login_system'
end
jigfox
My Application Controller has that already. class ApplicationController < ActionController::Base include LoginSystemendAny other suggestion?Ps. I tried changing the syntax to the one you showed above and I got an error "wrong argument type String (expected Module)"
mrbernz
A: 

You might also want to try devise. If you don't have some seriously different needs for login/logout accounts then it is great!

Sam