Thank you, but both method does not work.
Maby I'm stupid (This is my first month with RoR), but in my application
a user has only one role. So the first method does not work. And the
second method I don't understand. What do I have to fill in as
controller, and action? Also he said "{" is unexpected at your second
method.
I've also tried;
before_filter (:check_administrator_role || :check_taskmanager_role),
:only => [:administration]
Thank you, but both method does not work.
Maby I'm stupid (This is my first month with RoR), but in my
application
a user has only one role. So the first method does not work. And the
second method I don't understand. What do I have to fill in as
controller, and action? Also he said "{" is unexpected at your second
method.
I've also tried;
before_filter (:check_administrator_role || :check_taskmanager_role),
:only => [:administration]
You can't do anything like that. you need to produce a single filter
that performs the check (which seems to be what rick's suggestion is).
I think the problem was I didn't really go into how filters work. the
two existing check_xxx_role methods probably look something like:
def check_administrator_role
redirect_to somewhere unless user.has_role(:administrator)
end
def check_taskmanager_role
redirect_to somewhere unless user.has_role(:taskmanager)
end
So my simple admin_authorized method will actually stop the filter
chain unless the user has BOTH roles rather than either, instead
rather than calling the other two filter methods, it need to do
something like:
def admin_authorized
redirect_to somewhere unless user.has_role(:administrator) ||
user.has_role(:taskmanager)
end
I was about to suggest something along the lines of Rick's last
suggestion. Why note create a single method with a sponge parameter
so that it accepts one or more role names and returns whether or not
the user is one or more of those roles? If you did that then you
could include the method in your ApplicationController and share the
logic with all your controllers.
def authorized_for_roles(*roles)
roles.each{|role_name| return true if user.has_role?(role_name)}
false
end
With that you could have a before_filter like this:
hi
i am new to rails. i tried to create a site with multiple role as
described above.
when i tried the following code
def authorized_for_roles(*roles)
roles.each{|role| return true if @current_user.has_role?(role)}
permission_denied
end
i got the following error
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.has_role?