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. -- Posted via http://www.ruby-forum.com/.
On 2/20/06, Matt Southworth <ror@mmsw.org> wrote:> 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.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 Ludlow 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> -- Posted via http://www.ruby-forum.com/.
On 2/20/06, Matt Southworth <ror@mmsw.org> wrote:> James Ludlow wrote: > <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>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