# Share a subroutine with all controllers

**URL:** <https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370>\
**Category:** Rails\
**Created:** [December 18, 2009, 4:23pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370 "2009-12-18T16:23:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 18, 2009, 4:23pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/1 "2009-12-18T16:23:41Z")

</div>

Hi,  
I would like to create a subroutine to use in all my admin\_controller.

Actuall in my admin\_controller I have:

def acronym\_guide  
…  
@guide.description=@guide.description.gsub(/ PPC /," \<acronym  
title=#{34.chr}Pocket PC#{34.chr}\>PPC ")  
end

def acronym\_page  
…  
@page.description=@page.description.gsub(/ PPC /," \<acronym  
title=#{34.chr}Pocket PC#{34.chr}\>PPC ")  
end

I would like to write, for example, in admin\_helper (where I tried but  
it doesn’t work) a subroutine type:

def special\_chars(table,item)  
table.item=table.item.gsub(/ PPC /," \<acronym title=#{34.chr}Pocket  
PC#{34.chr}\>PPC ")  
end

that I can call in my admin\_controller as needed.

Thanks

---

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 18, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/2 "2009-12-18T17:34:58Z")

</div>

Helper methods aren’t available in the controllers by default. Why  
can’t you put it in admin\_controller like the others? Or, in  
application\_controller if you want it to be available to all  
controllers?

---

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 18, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/3 "2009-12-18T17:52:50Z")

</div>

I tried to put it in the application.rb, in admin\_controller but it  
doesn’t work!

Thank you

---

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 18, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/4 "2009-12-18T18:05:55Z")

</div>

Luigi M. wrote:

> I tried to put it in the application.rb, in admin\_controller but it  
> doesn’t work!
> 
> Thank you

Well, if you provide some details about the problem, then maybe someone  
can help you fix it. Don’t just give up and try to work around it.

---

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 18, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/5 "2009-12-18T18:53:08Z")

</div>

The error, that the application give me back, is:

undefined local variable or method `description’ for  
#AdminController:0x6756ac4.

I try to summarize:

In the admin\_helper.rb (but I tried also in application.rb and  
application\_helper) I put this subroutine:

def special\_chars(table,item)  
table.item=table.item.gsub(/ PPC /," \<acronym title=#{34.chr}Pocket  
PC#{34.chr}\>PPC ")  
end

In admin\_controller.rb I have defined some view where you can find:

def update  
@guide=Guide.find(params[:id])  
…  
special\_chars(@guide,description)  
…  
end

def uppage  
@page=Page.find(params[:id])  
…  
special\_chars(@page,text)  
…  
end

Thank you very much for your time

---

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 18, 2009, 9:52pm UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/6 "2009-12-18T21:52:43Z")

</div>

Ok, this is kind of strange -

def special\_chars(table,item)  
table.item=table.item.gsub(/ PPC /," \<acronym title=#{34.chr}Pocket  
PC#{34.chr}\>PPC ")  
end

You pass in a table and an item and then you change one of the  
attributes of the table. This is the wrong way to do this - whenever  
you change an object you should do it through a method of the object  
itself. eg

#in the Table class

def set\_item(item)  
self.item = item.gsub(/ PPC /," \<acronym title=#{34.chr}Pocket  
PC#{34.chr}\>PPC ")  
end

---

<div class="post-metadata">

**Author:** ![softwarewin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@softwarewin](https://www.ruby-forum.com/u/softwarewin)\
**Post date:** [December 19, 2009, 12:32am UTC](https://www.ruby-forum.com/t/share-a-subroutine-with-all-controllers/180370/7 "2009-12-19T00:32:16Z")

</div>

Thank Max for your time but,  
the snippet

table.item=table.item.gsub(/ PPC /," \<acronym title=#{34.chr}Pocket  
PC#{34.chr}\>PPC ")

that in my admin\_controller.rb is:

def acronym\_guide  
@guide=Guide.find(params[:id])  
@guide.description=@guide.description.gsub(/ PPC /," \<acronym  
title=#{34.chr}Pocket PC#{34.chr}\>PPC ")  
…  
if @guide.save  
flash[:notice]=‘Acronimi sostituiti.’  
redirect\_to :action=\>‘modifica’,:id=\>@guide  
end  
end

works fine.

My problem is that I need to do this operation (acronym substitution) in  
more points in the same controller and with a great number of ACRONYM.

So I would like to define a subroutine, as described before, to replace  
the acronym strings.

Thanks a lot
