Models can''t view the session at all. That''s bad MVC.
To do what you want, you just need to use good OOP.
def user_not_admin
return !is_admin?
end
You''re checking to see if the current user is an admin, correct? So
when you call a method on @session[:user] in your controller, then any
code in the class methods will operate on the object that gets called.
Looking back at that, it doesn''t make much sense to me...but I
can''t
really explain it a better way. Just read the pickaxe chapter on
objects and classes.
On 8/2/05, Cristiano Dias
<crisdias-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi there,
>
> I am using the Rails login generator user authentication model and it
> works fine so far. Now I want to be able to let my admins edit a user
> (specifically, give ''admin'' permissions with a click)
without having
> to worry about entering password and confirmation.
>
> So, I am trying to add the following condition (among others) to the User
model:
>
> validates_confirmation_of :password, :if => :user_not_admin
>
> Where user_not_admin would return true/false based on a user attribute
> I have called is_admin for the user currently logged in (not the user
> being changed).
>
> Right now I have:
>
> def user_not_admin
> return !(@session[:user].is_admin?)
> end
>
> This gives me an error even though the same
> "@session[:user].is_admin?" check works fine in controllers:
>
> "WARNING: You have a nil object when you probably didn''t
expect it!
> Odds are you
> want an instance of Array instead."
>
>
> So my guess is that models can''t see @session. Is that right? How
can
> I have this validates_confirmation_of :password, :if ... work?
>
>
>
> Thanks!
> CrisDias
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>