Detecting user roles and access

Taylor Strait wrote:

1) Is there a better function than this?

  def logged_in_as(role)
    if role == 'user'
      if session[:person][:id]
        return true
      else
        return false
      end
    elsif role =='admin'
      if session[:person][:id]
        if Person.find(session[:person][:id]).role == 'admin'
          return true
        else
          return false
        end
      end
    end
  end

2) My function doesn't work even though it is in application.rb:

ERROR:
undefined method `logged_in_as' for #<#<Class:0x4b9bdd4>:0x4b9bd84>

VIEW:
<% if logged_in_as('user') %>
  Some action
<% end %>

for 2), you cannot call controller methods directly from view, unless you define them as helper_method with helper_method :logged_in_as_user

Bojan Mihelac

Taylor Strait wrote:

Where does this go: helper_method :logged_in_as_user

It doesn't work in my application or People controller.

it should go after defining controller, for example:

class ApplicationController < ActionController::Base
   helper_method :tabs_menu
end

if it's defined in People controller it method would be visible only in its views.

best,
Bojan Mihelac