Rewriting with an after_filter

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.

On 2/20/06, Matt S. [email protected] 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

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

On 2/20/06, Matt S. [email protected] 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