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

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

Wes G. 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

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,
Wes

On 6/27/06, Wes G. [email protected] 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

On Jun 27, 2006, at 4:23 PM, Wes G. 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-
-Ezra

Ezra Z. wrote:

On Jun 27, 2006, at 4:23 PM, Wes G. 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-
-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?

Wes

Hi-

On Jun 27, 2006, at 5:21 PM, Wes G. wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Wes-

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