Hey, quick question about code organization I couldn''t answer in the wiki. I am storing user ids in sessions, and want to write a function that returns either the current user object, or constructs one for me and returns that. This will be for use both in my views and my controllers (where the logic is also useful). My question is whether it is appropriate to add this code to the helper if it is intended for use in the controller as well. None of the docs I''ve read, e.g.: http://wiki.rubyonrails.com/rails/pages/UnderstandingWhatFilesGoesWhere have been particularly clear about whether helpers are meant soley for simpifying view logic. Thanks, pt. -- Parker Thompson http://www.parkert.com/ 510.541.0125
On 9/28/05, Parker Thompson <parkert-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey, quick question about code organization I couldn''t answer in the > wiki. I am storing user ids in sessions, and want to write a function > that returns either the current user object, or constructs one for me > and returns that. This will be for use both in my views and my > controllers (where the logic is also useful). > > My question is whether it is appropriate to add this code to the > helper if it is intended for use in the controller as well. None of > the docs I''ve read, e.g.: > > http://wiki.rubyonrails.com/rails/pages/UnderstandingWhatFilesGoesWhere > > have been particularly clear about whether helpers are meant soley for > simpifying view logic. > > Thanks, >I define methods like that in my controller and insert them into the view helpers: helper_method :current_user def current_user @current_user ||= User.find_by_id(session[:user]) end -- rick http://techno-weenie.net
On 9/28/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > My question is whether it is appropriate to add this code to the > > helper if it is intended for use in the controller as well. > > I define methods like that in my controller and insert them into the > view helpers: > > helper_method :current_user > def current_user > @current_user ||= User.find_by_id(session[:user]) > endWhat I''d like to do is expose a helper function current_user to any controller/view that details with users (and includes helper :users). Will this do that (again, docs unclear)? If so, how is this different functionally from including it in the helper? Also, this seems to be an interesting way of adding helper functions, but if it really is a helper function what is the advantage of including it in the controller vs the helper? I''d be very interested in a broader discussion of the function above (everyone must have one), where they end up, and the relative merits of those locations. Thanks in advance for the help, just trying to save myself some refactoring time down the line :). pt. -- Parker Thompson http://www.parkert.com/ 510.541.0125 -- Parker Thompson http://www.parkert.com/ 510.541.0125
I do much the same, however I store the current user with flash.now, my logic being that it persists until the end of the current request, and will thus be available for rendered components (though I''ve not tested this myself yet). I do this in a base controller class that I inherit from for all controllers needing that particular functionality. On 28 Sep 2005, at 18:11, Parker Thompson wrote:> On 9/28/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>> My question is whether it is appropriate to add this code to the >>> helper if it is intended for use in the controller as well. >> >> I define methods like that in my controller and insert them into the >> view helpers: >> >> helper_method :current_user >> def current_user >> @current_user ||= User.find_by_id(session[:user]) >> end >