Strange NoMethodError exception with EDGE Rails

Hi guys,

I’ve encountered a strange exception which I can just not understand why is it happening (tried to trace it back to the rails source, but I’m not an expert, so it proved no results).

The situation is the following:

I have a simple module ported from acts_as_authenticated, modified here and there. I have 2 methods, namely logged_in? and superuser? defined as below:

module AuthenticatedSystem

module AuthenticatedSystem

protected

Preloads @current_user with the user model if they’re logged in.

def logged_in?
 current_user != :false
end

Return true if current_user is root (superuser)

def superuser? return false unless logged_in? current_user.roles.detect { |role| role.rights.detect { |right| right.controller == “" && right.action == "”}} end

end

The problem occurs in the views: I can use logged_in? just fine everywhere in the views, yet when using superuser? it throws a NoMethodError exception.

A sample view:

_navigation.erb

  • <%= link_to "Posts", posts_url %>
  • <% if superuser? %>

  • Administration
  • <% end %>

    <% unless logged_in? %>

  • Register Now!
  • <% else %>

  • <%= link_to “Logout”, logout_users_url %>
  • <% end %>

What I can’t understand is why logged_in? working and why superuser? is not, when they defined by the same means and at the same place.

Any advice would be welcome where should I look for solving this as I’m totally clueless already and just can’t read rails source well enough to debug it myself.

Thanks in advance, András

Hi András,

I have a simple module ported from acts_as_authenticated, modified here and there. I have 2 methods, namely logged_in? and superuser? defined as below:

# module AuthenticatedSystem module AuthenticatedSystem

[...]

   # Return true if current_user is root (superuser)    def superuser?     return false unless logged_in?      current_user.roles.detect { |role| role.rights.detect { |right| right.controller == "*" && right.action == "*"}}    end ... end

[...]

What I can't understand is why logged_in? working and why superuser? is not, when they defined by the same means and at the same place.

If you've taken the code from AuthenticatedSystem, you must have this method defined :

def self.included(base)   base.send :helper_method, :current_user, :logged_in? end

change it to :

def self.included(base)   base.send :helper_method, :current_user, :logged_in?, superuser? end

  -- Jean-François.