Re: Login controller additions: SOLUTION

Jonas Montonen
Thu, 10 Mar 2005 16:17:41 -0000

Jonas Montonen wrote:

> Jonas Montonen wrote:
>
>> I have added roles and roles_users table and updated the model so
>> that my users can have multiple roles.
>> ("Admin" role does always have id = 1).
>>
>> I have added these methods to my application controller.
>>
>>  helper_method :is_admin?
>>  helper_method :is_user?
>>
>>  def is_admin?
>>      if @session[''user'']
>>        @session[''user''].roles.find(1)
>>      else
>>        false
>>      end    end
>>
>>  def is_user?
>>    !@session[''user''].nil?
>>  end
>>
>>
>> The problem is that if a user does not have the role "admin"
(id=1) the
>> @session[''user''].roles.find(1) statment fails with
error "unable to
>> find role with id = 1".
>>
>> How can I do this another way..... I know it must be simple... but
>> I''m really a Rails/Ruby newbie ;)
>>
>> Regards
>> // JoNtE
>>
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
> @session[''user''].roles.include?(1)
> Workd for me... I guess both solutions suggested would have worked as
> well.
>
> Thanx Jarkko and Nicholas for the quick answers
>
> // JoNtE
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Well... after som testing my solution did not work after all... but
Jarkko''s did.
Great!

@session[''user''].roles.collect{ |role| role.id }.include?(1)

// JoNtE



_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org