Wes Gamble
2006-Jun-27 23:18 UTC
[Rails] Can''t call public application.rb method from ERb template
All, I have a left navigation partial that I want to dynamically generate CSS classes for based on the current controller action. In my ERb template, I have <DIV class="<%= get_menu_display_style(''login_form'') %>"> In application.rb, I have the method get_menu_display_style defined as: public def get_menu_display_style(action_requested) action_name.eql(action_requested) ? ''left_nav_button_selected'' : ''left_nav_button'' end But when I go to my initial page, I get undefined method `get_menu_display_style'' for #<#<Class:0x37272b0>:0x3727220> I''m confused. I figured that I would have access to the application controller methods from any ERb template - is this wrong? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jun-27 23:26 UTC
[Rails] Re: Can''t call public application.rb method from ERb templat
Wes Gamble wrote:> All, > > I have a left navigation partial that I want to dynamically generate CSS > classes for based on the current controller action. > > In my ERb template, I have > > <DIV class="<%= get_menu_display_style(''login_form'') %>"> > > In application.rb, I have the method get_menu_display_style defined as: > > public > def get_menu_display_style(action_requested) > action_name.eql(action_requested) ? ''left_nav_button_selected'' : > ''left_nav_button'' > end > > But when I go to my initial page, I get > > undefined method `get_menu_display_style'' for > #<#<Class:0x37272b0>:0x3727220> > > I''m confused. I figured that I would have access to the application > controller methods from any ERb template - is this wrong? > > Thanks, > WesI was able to make this working by calling controller.get_menu_display_style in my template. However, I still don''t understand something. How can I call methods like render from my template without the need for specifying the controller built-in variable??? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Jeremy Evans
2006-Jun-27 23:49 UTC
[Rails] Can''t call public application.rb method from ERb template
On 6/27/06, Wes Gamble <weyus@att.net> wrote:> I''m confused. I figured that I would have access to the application > controller methods from any ERb template - is this wrong?Yes. Add the method to application_helper.rb instead of application.rb. If you need to call it inside the controller as well as the view, keep it in application.rb and use helper_method [1]. [1] http://rubyonrails.org/api/classes/ActionController/Helpers/ClassMethods.html#M000139
Ezra Zygmuntowicz
2006-Jun-28 00:04 UTC
[Rails] Re: Can''t call public application.rb method from ERb templat
On Jun 27, 2006, at 4:23 PM, Wes Gamble wrote:> Wes Gamble wrote: >> All, >> >> I have a left navigation partial that I want to dynamically >> generate CSS >> classes for based on the current controller action. >> >> In my ERb template, I have >> >> <DIV class="<%= get_menu_display_style(''login_form'') %>"> >> >> In application.rb, I have the method get_menu_display_style >> defined as: >> >> public >> def get_menu_display_style(action_requested) >> action_name.eql(action_requested) ? ''left_nav_button_selected'' : >> ''left_nav_button'' >> end >> >> But when I go to my initial page, I get >> >> undefined method `get_menu_display_style'' for >> #<#<Class:0x37272b0>:0x3727220> >> >> I''m confused. I figured that I would have access to the application >> controller methods from any ERb template - is this wrong? >> >> Thanks, >> Wes > > > I was able to make this working by calling > > controller.get_menu_display_style > > in my template. > > However, I still don''t understand something. How can I call methods > like render from my template without the need for specifying the > controller built-in variable??? > > Thanks, > WesWes- One way to do what you want is to declare the method in application.rb as a helper method. The reason you have to use controller.method_name is that the controller is a different object then your view is. So try this instead: class ApplicationController < ActionController::Base def get_menu_display_style(action_requested) action_name.eql(action_requested) ? ''left_nav_button_selected'' : ''left_nav_button'' end helper_method :get_menu_display_style end Cheers- -Ezra
Wes Gamble
2006-Jun-28 00:21 UTC
[Rails] Re: Re: Can''t call public application.rb method from ERb tem
Ezra Zygmuntowicz wrote:> On Jun 27, 2006, at 4:23 PM, Wes Gamble wrote: > >>> >>> >> I was able to make this working by calling >> Wes > Wes- > > One way to do what you want is to declare the method in > application.rb as a helper method. The reason you have to use > controller.method_name is that the controller is a different object > then your view is. So try this instead: > > > class ApplicationController < ActionController::Base > > def get_menu_display_style(action_requested) > action_name.eql(action_requested) ? ''left_nav_button_selected'' : > ''left_nav_button'' > end > helper_method :get_menu_display_style > > end > > > Cheers- > -EzraEzra, Got it. So, at some point is the "render" method declared internally as a helper method and that''s why I can use it from a template? Wes -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Jun-28 15:29 UTC
[Rails] Re: Re: Can''t call public application.rb method from ERb tem
Hi- On Jun 27, 2006, at 5:21 PM, Wes Gamble wrote:> > Ezra, > > Got it. > > So, at some point is the "render" method declared internally as a > helper > method and that''s why I can use it from a template? > > Wes > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsWes- Actually ActionController::Base and ActionView::Base both have their own implementations of render. vendor/rails/actionpack/lib/action_view/base.rb line 262 vendor/rails/actionpack/lib/action_controller/base.rb line 613 -Ezra