# How to override one method of AssetTagHelper

**URL:** <https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985>\
**Category:** Rails\
**Created:** [October 28, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985 "2008-10-28T18:46:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/1 "2008-10-28T18:46:42Z")

</div>

I created a file ./lib/action\_view/helpers/asset\_tag\_helper.rb

and put in it only the method I want to override. like this

module ActionView  
module Helpers #:nodoc:  
module AssetTagHelper  
def image\_path(source)  
compute\_public\_path(source, ‘images’)  
end  
end  
end  
end

But as soon I try this all the others methods from the overriden module  
are not available anymore.

Any idea?

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/2 "2008-10-28T19:06:15Z")

</div>

On 28 Oct 2008, at 17:46, RÃ©mi Gagnon \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> I created a file ./lib/action\_view/helpers/asset\_tag\_helper.rb
> 
> and put in it only the method I want to override. like this

Don’t. Create lib/my\_module.rb and then include MyModule into  
ActionView::Base. Or it might be more convenient to do this in  
application\_helper

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/3 "2008-10-28T19:18:25Z")

</div>

Thank you, you are really right I should put it in my  
application\_helper. But Here we will have a lot of projets and I want  
to share this kind of changes to all teams. So that’s why I would like  
to ultimatly put it in a gem.

So I’m curious to see how you would include mymodule on  
ActionView::Base.

Thanks in advance

Frederick C. wrote:

> On 28 Oct 2008, at 17:46, RÃ©mi Gagnon \<rails-mailing-list@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > I created a file ./lib/action\_view/helpers/asset\_tag\_helper.rb
> > 
> > and put in it only the method I want to override. like this
> 
> Don’t. Create lib/my\_module.rb and then include MyModule into  
> ActionView::Base. Or it might be more convenient to do this in  
> application\_helper

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/4 "2008-10-28T20:57:50Z")

</div>

On Oct 28, 6:18 pm, Rémi Gagnon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you, you are really right I should put it in my  
> application\_helper. But Here we will have a lot of projets and I want  
> to share this kind of changes to all teams. So that’s why I would like  
> to ultimatly put it in a gem.
> 
> So I’m curious to see how you would include mymodule on  
> ActionView::Base.

ActionView::Base.send :include, MyModule

With a plugin you’d typically put this in the plugin’s init.rb

Fred

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/5 "2008-10-28T22:37:13Z")

</div>

Hi,

In our project we are using lib file as suggested

file name would be:  
lib/action\_view\_ext.rb

module ActionView  
module Helpers #:nodoc:  
module AssetTagHelper  
def image\_path(source)  
compute\_public\_path(source, ‘images’)  
end  
end  
end  
end

the best way to include the file !always! (doesn’t matter if the  
application helper is loaded or not)  
is to add one line at the end of the

config/environment.rb

the line would be:

require ‘lib/action\_view\_ext.rb’

Cheers

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/6 "2008-10-28T22:44:58Z")

</div>

Thanks a lot for the quick response!

BTW the reason why I want to override a method is because I need to  
support images for different locale.

for instance :

logo\_fr.gif (for french)  
logo\_en.gif (for of course english)

In my view I want something like :

\<% image\_tag(‘logo.gif’, :locale =\> cookies[:locale])%\>

any better way?

Thanks again

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/7 "2008-10-28T23:13:00Z")

</div>

> In my view I want something like :
> 
> \<% image\_tag(‘logo.gif’, :locale =\> cookies[:locale])%\>
> 
> any better way?

Will all the pictures on the site be localized?  
Probably not - maybe it would be better if you create a method called  
something like

image\_localized\_tag

in your application helper. It could take “locale” from cookie and  
change passed file name (locale isn’t parameter now).  
After changing the name it could just call image\_tag and return the  
result.

This is just a suggestion - IMHO it’s easier to implement and cleaner  
later on for other developers - some of them may expect different  
behaviour from image\_tag

cheers

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [October 28, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/how-to-override-one-method-of-assettaghelper/151985/8 "2008-10-28T23:26:14Z")

</div>

After rethinking of it that’s what I plan to do. Just wnat to make sure  
it is the right way and I wanted assements of experts.

Thank you very much.
