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
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???
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].
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
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-
-Ezra
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?