Detecting user roles and access

  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

  1. 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 %>

Taylor S. wrote:

  if session[:person][:id]

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 M.


Bojan M.
Informatika Mihelac, Bojan M. s.p. | www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org

Where does this go: helper_method :logged_in_as_user

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

Taylor S. 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 M.


Bojan M.
Informatika Mihelac, Bojan M. s.p. | www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.mihelac.org