Mathew Hennessy
2006-Feb-19 19:27 UTC
[Rails] instance variables in components not read by component view?
Hi, I''m trying to write a sidebar menu with dynamic menus, and to do so I would like to define a list of menus to display within my sidebar component controller code and pass that list to the component display view, like so: # menu_controller.rb class Sidebar::MenuController < ActionController::Base uses_component_template_root @menus = %w{admin user help} def display @display_menu = params[:menu] render(:layout => false) end end # menu/display.rhtml <div class="menu"> <% @menus.each do |menu| %> <%= link_to_remote "#{menu}",:url => { :controller => "sidebar/menu", :action => ''toggle_menu'', :menu => "#{menu}" } %> <br> <div id="<%= menu %>_menu" style="display: none"> <%= render_component(:controller => "sidebar/menu", :action => "#{menu}_menu") %> </div> <% end %> </div> When trying to run display.rhtml, I get errors saying @menu == nil instead of Array. Now if I replace the display @menus loop with this: <% (%w{admin user help}).each do |menu| %> it works as expected. I''ve tried putting @menus both within the display method and outside it in the class definition. Am I not understanding the component concept correctly? The Rails book demonstrates component controller classes passing instance variables to the views (pp. 376-377, look for get_links and @links) so what am I missing? TYVMIA, - Matt -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Feb-19 21:33 UTC
[Rails] instance variables in components not read by component view?
Matthew- You need to put the menu initialization in a before filter. Then it will work like you want.: # menu_controller.rb class Sidebar::MenuController < ActionController::Base uses_component_template_root before_filter :setup_menu attr_accessor :menu def display @display_menu = params[:menu] render(:layout => false) end private def setup_menu @menus = %w{admin user help} end end That will do exactly what you want. Cheers- -Ezra On Feb 19, 2006, at 11:27 AM, Mathew Hennessy wrote:> Hi, > > I''m trying to write a sidebar menu with dynamic menus, and to do so I > would like to define a list of menus to display within my sidebar > component controller code and pass that list to the component display > view, like so: > > # menu_controller.rb > class Sidebar::MenuController < ActionController::Base > uses_component_template_root > > @menus = %w{admin user help} > > def display > @display_menu = params[:menu] > render(:layout => false) > end > end > > # menu/display.rhtml > <div class="menu"> > <% @menus.each do |menu| %> > <%= link_to_remote "#{menu}",:url => { :controller => "sidebar/menu", > :action => ''toggle_menu'', :menu => "#{menu}" } %> <br> > <div id="<%= menu %>_menu" style="display: none"> > <%= render_component(:controller => "sidebar/menu", :action => > "#{menu}_menu") %> > </div> > <% end %> > </div> > > When trying to run display.rhtml, I get errors saying @menu == nil > instead of Array. Now if I replace the display @menus loop with this: > > <% (%w{admin user help}).each do |menu| %> > > it works as expected. > > I''ve tried putting @menus both within the display method and > outside it > in the class definition. Am I not understanding the component concept > correctly? The Rails book demonstrates component controller classes > passing instance variables to the views (pp. 376-377, look for > get_links > and @links) so what am I missing? > > TYVMIA, > - Matt > > -- > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra@yakima-herald.com 509-577-7732