Michal Burak
2009-Sep-25 11:45 UTC
calling partials from a layout - once nil, once not nil
Hi, Rails 2.3.3 + restful_authentication. current_user is a method from lib/authenticated_system.rb through "include AuthenticatedSystem" in application_controller.rb -------- home/_current_user.pl.html.erb : -------- <% if current_user %> NOT NIL <% else %> NIL <% end %> -------- home/_user_box.pl.html.erb : -------- <% if current_user %> NOT NIL <% else %> NIL <% end %> -------- layouts/layout.html.erb : -------- <%= render :partial => ''/home/current_user'' -%> <%= render :partial => ''/home/user_box'' %> ----- OUTPUT: ------- NIL NOT NIL ---- LOGS: ------- [INFO : 0925 13:43:58 #9238] Rendering template within layouts/layout [INFO : 0925 13:43:58 #9238] Rendering home/index [DEBUG: 0925 13:43:58 #9238] Rendered home/_current_user.pl (0.2ms) [DEBUG: 0925 13:43:58 #9238] User Columns (1.9ms) SHOW FIELDS FROM `users` [DEBUG: 0925 13:43:58 #9238] User Load (0.2ms) SELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [DEBUG: 0925 13:43:58 #9238] Rendered home/_user_box.pl (6.9ms) [INFO : 0925 13:43:58 #9238] Completed in 16ms (View: 8, DB: 2) | 200 OK [http://localhost/] Why is that so? Why one time it is nil and the other time it is not nil? Any help appreciated. Regards. -- Posted via http://www.ruby-forum.com/.
Ilan Berci
2009-Sep-25 12:50 UTC
Re: calling partials from a layout - once nil, once not nil
Michal Burak wrote:> > -------- home/_current_user.pl.html.erb : -------- > <% if current_user %> > NOT NIL > <% else %> > NIL > <% end %> > > -------- home/_user_box.pl.html.erb : -------- > <% if current_user %> > NOT NIL > <% else %> > NIL > <% end %> > > -------- layouts/layout.html.erb : -------- > <%= render :partial => ''/home/current_user'' -%> > <%= render :partial => ''/home/user_box'' %> >> > Why is that so? Why one time it is nil and the other time it is not nil? > > Any help appreciated. > > Regards.It has to do with the name of the partial, Rails does some magic by looking at the name of the partial and then making a local variable (with the same name as the partial) accessible to that partial. i.e. a partial entitled "_current_user" will have current_user as a local whereas a partial entitled "_user_box" will have user_box as a local. This was new functionality introduced with rails 2 I believe.. Don''t forget that the render() can have :object and :locals passed into it.. hth ilan -- Posted via http://www.ruby-forum.com/.