Hi, I am using Restful authentication for user authentication. I want the current logged in user object in my model property.rb Problem is current_user is not working on model. How can I fix this I need that? Please help me out. Thanks, Mike -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 Sep 2010, at 09:31, Mike Disuza wrote:> I am using Restful authentication for user authentication. > I want the current logged in user object in my model property.rb > Problem is current_user is not working on model. > > How can I fix this I need that?You have two options: 1. Pass the current user from your controller into your model method: some_model.some_method(current_user) 2. Use threads to set a class variable on the User model. You can either use the userstamp gem/plugin to provide you the functionality (User.stamper) or extract the necessary code and roll your own: http://github.com/grosser/userstamp The first method respects the separation of MVC better, the second can be more convenient in some cases. It''s up to you to decide the best route. Best regards Peter De Berdt -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Mike Disuza wrote:> Hi, > I am using Restful authentication for user authentication.[...] This isn''t directly related to your issue...but if you don''t mind a bit of advice, I highly recommend removing restful_authentication from your project as soon as possible. It relies on unmaintainable generated code, and so is needlessly hard to work with. I recommend Authlogic instead.> > Thanks, > MikeBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2010-Sep-16 19:04 UTC
Re: Resyful authenticatio current_user in model
The third option is to make virtual attribute on the model and always set it when needed in the controller class Model attr_accessor :current_user def some_method if current_user.admin? #foo else #bar end end end class FoosController def index m = Model.find(...) m.current_user = current_user m.some_method() end end Marnen: What''s so ''unmaintainable'' in your opinion? Robert Pankowecki -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.