Share a subroutine with all controllers

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

@[email protected](/ PPC /," <acronym
title=#{34.chr}Pocket PC#{34.chr}>PPC ")
end

def acronym_page

@[email protected](/ 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

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?

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

Thank you

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.

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

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

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])
@[email protected](/ 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