On 27 February 2010 17:09, robo <[email protected]> wrote:
> I should note that @user is a variable set by my ApplicationController
> using a before_filter.  really sure what's going on here, so any help
> is appreciated.  I'm guessing the @user set by my app controller
> probably isn't accessible by the Post class.

Yup - you've identified your problem perfectly... here's a suggested solution:

@user is a instance variable; that is, only available to the instance
of a class (in this instance, the controller). So you can't access it
from an instance of a different class.
So you need to create a method on your controller that will return the
@user object. The convention for this method seems to be to call it
"current_user".

def current_user
  @user
end

Simple, huh? ;-)
Now you can get rid of that before_filter call, and do the operation
when/if somewhere calls "current_user" - so it'll look more like this:

def current_user
  @user ||= the_method_for_the_logic_that_works_out_the_logged_in_user
end


An other solution would be to use a wrapped-up plugin to do
authentication (there are lots to choose from), which would take all
you user/password management woes and deal with them for you (and most
likely give you a "current_user" method!)

HTH
Michael

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to