A really smart rails helper needed

Posted by Stefan Liebenberg on Stack Overflow See other posts from Stack Overflow or by Stefan Liebenberg
Published on 2010-03-07T20:45:53Z Indexed on 2010/03/08 7:21 UTC
Read the original article Hit count: 384

Filed under:
|

In my rails application I have a helper function:

def render_page( permalink )
   page = Page.find_by_permalink( permalink )
   content_tag( :h3, page.title ) + inline_render( page.body )
end

If I called the page "home" with:

<%= render_page :home %>

and "home" page's body was:

<h1>Home<h1/>
bla bla

<%= render_page :about %>
<%= render_page :contact %>

I would get "home" page, with "about" and "contact", it's nice and simple... right up to where someone goes and changes the "home" page's content to:

<h1>Home<h1/>
bla bla
<%= render_page :home %>    
<%= render_page :about %>
<%= render_page :contact %>

which will result in a infinite loop ( a segment fault on webrick )...

How would I change the helper function to something that won't fall into this trap?

My first attempt was along the lines of:

@@list = []

def render_page( permalink )
  unless @@list.include?(permalink)
    @@list += [ permalink ]
    page = Page.find_by_permalink
    result = content_tag( :h3, page.title ) + inline_render( page.body )
    @@list -= [ permalink ]
    return result
  else
    content_tag :b, "this page is already being rendered"
  end
end

which worked on my development environment, but bombed out in production...

any suggestions?

Thank You
Stefan

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby