Forum: Ruby on Rails Rewriting with an after_filter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt S. (Guest)
on 2006-02-21 00:35
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
<customer/> in the response body)."

This, "substituting a customerĂ¢??s name for the string <customer/>", 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:

<code>
class DisplayController < ApplicationController

  after_filter :customerize

  def show
    @contentdivs = Contentdiv.thispage(params[:page])
  end

  def customerize
	  @contentdivs.gsub(/UserName/, 'Matthew')
  end


end
</code>

@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.
James L. (Guest)
on 2006-02-21 00:54
(Received via mailing list)
On 2/20/06, Matt S. <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
Matt S. (Guest)
on 2006-02-21 03:49
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.


<code>
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
</code>
James L. (Guest)
on 2006-02-21 19:57
(Received via mailing list)
On 2/20/06, Matt S. <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
> </code>

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
This topic is locked and can not be replied to.