# Can't call public application.rb method from ERb template

**URL:** <https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306>\
**Category:** Rails\
**Created:** [June 28, 2006, 1:18am UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306 "2006-06-28T01:18:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 28, 2006, 1:18am UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306/1 "2006-06-28T01:18:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 28, 2006, 1:23am UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306/2 "2006-06-28T01:23:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 28, 2006, 1:52am UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306/3 "2006-06-28T01:52:11Z")

</div>

On 6/27/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://rubyonrails.org/api/classes/ActionController/Helpers/ClassMethods.html#M000139)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 28, 2006, 2:07am UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306/4 "2006-06-28T02:07:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 28, 2006, 2:21am UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306/5 "2006-06-28T02:21:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 28, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/cant-call-public-application-rb-method-from-erb-template/96306/6 "2006-06-28T17:32:07Z")

</div>

Hi-

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

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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
