Hi, Is it possible for Views to call the Controller''s public methods? I have the current user stored inside a session: session[:current_user] Now in the Views instead of hardwiring to session[:current_user] I would like to use something simple like "current_user" so I created that method in the ApplicationController class ApplicationController < ActionController::Base model :player public def current_user session[:current_user] end end Using this in the views gives me an error: undefined local variable or method `current_user'' for #<ActionView::Base:0x36b0aa0> Any ideas? Jeroen
I think these types of methods belong in the helper classes/modules for the controllers. I believe the view has access to all of those methods. -Nick On 10/17/05, Jeroen Houben <jeroen-aHd7JyfBtzlmR6Xm/wNWPw@public.gmane.org> wrote:> Hi, > > Is it possible for Views to call the Controller''s public methods? > > I have the current user stored inside a session: session[:current_user] > > Now in the Views instead of hardwiring to session[:current_user] I would > like to use something simple like "current_user" so I created that > method in the ApplicationController > > class ApplicationController < ActionController::Base > > model :player > > public > def current_user > session[:current_user] > end > > end > > Using this in the views gives me an error: > undefined local variable or method `current_user'' for > #<ActionView::Base:0x36b0aa0> > > Any ideas? > > Jeroen > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
in your controller you can do the following: def index @current_user = session[:current_user] end and then @current_user in your view regards, Francois Paul Jeroen Houben wrote:> Hi, > > Is it possible for Views to call the Controller''s public methods? > > I have the current user stored inside a session: session[:current_user] > > Now in the Views instead of hardwiring to session[:current_user] I > would like to use something simple like "current_user" so I created > that method in the ApplicationController > > class ApplicationController < ActionController::Base > > model :player > > public > def current_user > session[:current_user] > end > > end > > Using this in the views gives me an error: > undefined local variable or method `current_user'' for > #<ActionView::Base:0x36b0aa0> > > Any ideas? > > Jeroen > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Francois Paul wrote:> in your controller you can do the following: > > def index > @current_user = session[:current_user] > end > > and then @current_user in your viewThanks Francois, This indeed works, but is a bit cumbersome. I have to use @current_user = session[:current_user] everytime I want to use it in some view. I''ve now specified def current_user session[:current_user] end In my ApplicationHelper class and it''s now available in all Views automatically. It''s however not automatically available in every Controller, which is a shame.. Jeroen
You can put it in your controller then do: helper_method :current_user After it and it will be available in both the controller and the view. Chris On 10/17/05, Jeroen Houben <jeroen-aHd7JyfBtzlmR6Xm/wNWPw@public.gmane.org> wrote:> Francois Paul wrote: > > in your controller you can do the following: > > > > def index > > @current_user = session[:current_user] > > end > > > > and then @current_user in your view > > Thanks Francois, > > This indeed works, but is a bit cumbersome. I have to use @current_user > = session[:current_user] everytime I want to use it in some view. > > I''ve now specified > > def current_user > session[:current_user] > end > > In my ApplicationHelper class and it''s now available in all Views > automatically. > > It''s however not automatically available in every Controller, which is a > shame.. > > Jeroen > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Chris McGrath wrote:> You can put it in your controller then do: > > helper_method :current_user > > After it and it will be available in both the controller and the view.Excellent! Could you please explain why this works? How does the method become available to the views? Does it use Ruby''s mixin mechanism? Jeroen
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 17, 2005, at 6:48 AM, Jeroen Houben wrote:> Chris McGrath wrote: >> You can put it in your controller then do: >> helper_method :current_user >> After it and it will be available in both the controller and the >> view. > > Excellent! Could you please explain why this works? How does the > method become available to the views? Does it use Ruby''s mixin > mechanism?http://api.rubyonrails.org/classes/ActionController/Helpers/ ClassMethods.html The way helper_method works is by proxying the controller method in the view helper. It automatically creates a similar method to the one you made by hand. Before: class MyController < ApplicationController protected # Use a protected or private method since public methods are also web actions. def current_user session[:current_user] end end module MyHelper def current_user controller.send :current_user end end After: class MyController < ApplicationController helper_method :current_user protected # Use a protected or private method since public methods are also web actions. def current_user session[:current_user] end end Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDU67KAQHALep9HFYRAjKPAKDMnxrl+AfJ7auQbJqqSYzAA64XFwCfVBDc vZInynpqIeh/ucxn8sH80+0=JzLM -----END PGP SIGNATURE-----