# Rewriting with an after\_filter

**URL:** <https://www.ruby-forum.com/t/rewriting-with-an-after-filter/71046>\
**Category:** Rails\
**Created:** [February 20, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/rewriting-with-an-after-filter/71046 "2006-02-20T23:35:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 20, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/rewriting-with-an-after-filter/71046/1 "2006-02-20T23:35:19Z")

</div>

All -

I’m completely new to RoR and also new to programming in an MVC  
structure…

The Pragmatic Programmers guide to Rails says, temptingly:

“After filters can be used to modify the outbound response, changing the  
headers and content if required. Some applications use this technique to  
perform global replacements in the content generated by the controllerâ??s  
templates (for example, substituting a customerâ??s name for the string  
 in the response body).”

This, "substituting a customerâ??s name for the string ", is  
exactly the simple task I’m trying to accomplish, however I can’t find  
how to do it.

I think what I want to do is gsub(/UserName/, ‘Matthew’), however, I’m  
clearly missing something. Here’s what I use:

`
class DisplayController < ApplicationController
after_filter :customerize
def show
@contentdivs = Contentdiv.thispage(params[:page])
end
def customerize
@contentdivs.gsub(/UserName/, ‘Matthew’)
end
end
`

@contentdivs contains bits of html that show.rhtml nicely formats.  
thispage() selects only those with an attribute that matches the page  
requested.

The error I get is “private method `gsub’ called for  
#Array:0x385df18”. I’m certain the solution is blindingly simple.

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 20, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/rewriting-with-an-after-filter/71046/2 "2006-02-20T23:54:27Z")

</div>

On 2/20/06, Matt S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> thispage() selects only those with an attribute that matches the page  
> requested.
> 
> The error I get is “private method `gsub’ called for  
> #Array:0x385df18”. I’m certain the solution is blindingly simple.

gsub is a String method, but you appear to have an array of strings.

Try something like this instead:

@contentdivs.each { |div| div.gsub!(/UserName/, ‘Matthew’) }

– James

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 21, 2006, 2:49am UTC](https://www.ruby-forum.com/t/rewriting-with-an-after-filter/71046/3 "2006-02-21T02:49:47Z")

</div>

James L. wrote:

> @contentdivs.each { |div| div.gsub!(/UserName/, ‘Matthew’) }

Thanks! I’m getting closer. This:

```
@contentdivs.each { |div| div.content.gsub!(/UserName/, 'Matthew') }

```

Works in the show method, but not in an after\_filter. I’ve put a code  
snippet below. When I try to do that in an after\_filter, I get no error,  
but no substitution.

`
class DisplayController < ApplicationController
#when the below is uncommented, it has no effect:
#after_filter :customerize
def show
@contentdivs = Contentdiv.thispage(params[:page])
# the below line works in show but not the after_filter
@contentdivs.each { |div| div.content.gsub!(/UserName/, ‘Matthew’) }
end
#when the below is uncommented, it has no effect:
#def customerize
#@contentdivs.each { |div| div.content.gsub!(/UserName/, ‘Matthew’) }
#end
end
`

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 21, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/rewriting-with-an-after-filter/71046/4 "2006-02-21T18:57:36Z")

</div>

On 2/20/06, Matt S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @contentdivs.each { |div| div.content.gsub!(/UserName/, 'Matthew') }
> 
> ```
> 
> end
> 
> #when the below is uncommented, it has no effect:  
> #def customerize  
> #@contentdivs.each { |div| div.content.gsub!(/UserName/, ‘Matthew’) }  
> #end
> 
> end

Sorry, it should have registered with me the first time that you were  
doing this as an after\_filter. In your after\_filter method your array  
has already been used to generate the response, so changing it won’t  
matter.

Try this instead:

def customerize  
response.body.gsub!(/UserName/, ‘Matthew’)  
end

– James
