# Problem with virtual page(s) and find\_by\_url

**URL:** <https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503>\
**Category:** Radiant CMS\
**Created:** [January 29, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503 "2008-01-29T21:33:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![foobar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@foobar](https://www.ruby-forum.com/u/foobar)\
**Post date:** [January 29, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503/1 "2008-01-29T21:33:29Z")

</div>

I have the following setup:

Home Page  
|  
– People (PeoplePage)  
|  
– Person (PersonPage)  
|  
– Bio (BioPage)  
– Contact (ContactInfoPage)

I made an extension with some Page Types (in parentheses) wich are used  
to  
display the list of people in the company and the data about a  
particular  
person, respectively (the latter being a virtual page).

Since a person object carries plenty of data I need to split the data  
into  
several pages, like:

/people/18 # 18 is an ID of a existing object  
(Person)  
/people/18/bio  
/people/18/contact  
…

It seems I have a problem with find\_by\_url in PersonPage:

def virtual?  
true  
end

def find\_by\_url(url, live = true, clean = true)  
url = clean\_url(url) if clean  
if url =~ %r{#{parent.url}/\d+/bio/?$}  
children.find\_by\_class\_name ‘BioPage’  
else  
super  
end  
end

This looks fine to my untrained eye, but:

$ script/console  
Loading development environment.

> > p = Page.find\_by\_url(’/people/18/bio/’)  
> > =\> #\<FileNotFoundPage:0xb709db54 @attributes={“virtual”=\>“1”,  
> > “class\_name”=\>“FileNotFoundPage”, “slug”=\>“file-not-found”,  
> > “updated\_at”=\>“2008-01-24 00:47:14”, “title”=\>“File Not Found”,  
> > “created\_by”=\>“1”, “breadcrumb”=\>“File Not Found”, “lock\_version”=\>“2”,  
> > “updated\_by”=\>“1”, “id”=\>“3”, “published\_at”=\>“2008-01-20 23:51:27”,  
> > “status\_id”=\>“100”, “layout\_id”=\>nil, “parent\_id”=\>“2”,  
> > “created\_at”=\>“2008-01-20 22:51:27”}\>

Then, this works in the same console:

p = Page.find\_by\_url(’/people/18/’)  
p.children.find\_by\_class\_name ‘BioPage’

Regexp looks fine too, as far as I can tell. I know this is a cockpit  
problem, but I need someone to point a finger at it, since I must be  
(code)  
blind or something. BioPage is declared virtual, too (if it matters).  
Can  
anyone suggest what could be wrong?

---

<div class="post-metadata">

**Author:** ![foobar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@foobar](https://www.ruby-forum.com/u/foobar)\
**Post date:** [January 29, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503/2 "2008-01-29T21:42:23Z")

</div>

It seems it might be appropriate for your PersonPage to be virtual and  
the PeoplePage to override find\_by\_url as well.

Sean

---

<div class="post-metadata">

**Author:** ![foobar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@foobar](https://www.ruby-forum.com/u/foobar)\
**Post date:** [January 29, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503/3 "2008-01-29T22:12:02Z")

</div>

On Jan 29, 2008 9:42 PM, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems it might be appropriate for your PersonPage to be virtual and  
> the PeoplePage to override find\_by\_url as well.

Hi Sean,

Thanks for the reply. Both of the PersonPage and the BioPage are  
virtual,  
and in PeoplePage I have:

def find\_by\_url(url, live = true, clean = false)  
url = clean\_url(url) if clean  
if url =~ %r{#{self.url}/?\d+/?$}  
children.find\_by\_class\_name ‘PersonPage’  
else  
super  
end  
end

def child\_url(child)  
clean\_url “#{url}/#{child.id}”  
end

I can get the pages by Page.find\_by\_url for the

/people/  
/people/18

but it doesn’t work for

/people/18/bio

Its a wildest guess, but does it have anything to do with the fact that  
I’m  
redefining find\_by\_url in a virtual page (per se)? I’d say no, but just  
to  
be on the safe side…

nn wrote:

> I made an extension with some Page Types (in parentheses) wich are used to  
> display the list of people in the company and the data about a particular  
> person, respectively (the latter being a virtual page).
> 
> Since a person object carries plenty of data I need to split the data into  
> several pages, like:
> 
> /people/18 # 18 is an ID of a existing object  
> (Person)  
> def find\_by\_url(url, live = true, clean = true)  
> $ script/console  
> “created\_at”=\>“2008-01-20 22:51:27”}\>
> 
> Then, this works in the same console:
> 
> p = Page.find\_by\_url(‘/people/18/’)  
> p.children.find\_by\_class\_name ‘BioPage’
> 
> Regexp looks fine too, as far as I can tell. I know this is a cockpit  
> problem, but I need someone to point a finger at it, since I must be  
> (code)  
> blind or something. BioPage is declared virtual, too (if it matters). Can  
> anyone suggest what could be wrong?

---

<div class="post-metadata">

**Author:** ![foobar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@foobar](https://www.ruby-forum.com/u/foobar)\
**Post date:** [January 29, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503/4 "2008-01-29T22:27:23Z")

</div>

In this find\_by\_url method, you return the PersonPage if the URL is  
/people/18, but you don’t call its find\_by\_url if the URL is  
/people/18/bio. Remove the $ from the regexp and then instead of  
returning children.find\_by\_class\_name(‘PersonPage’), return  
children.find\_by\_class\_name(‘PersonPage’).find\_by\_url(url, live,  
clean). Then in your PersonPage, have several tests – one that matches  
for itself (use the one below and substitute parent.url for self.url),  
and then any special cases (/bio, /contact, etc) that return the  
appropriate sub-page, then super.

If this is unclear, write me back and I’ll give more detail.

Sean

---

<div class="post-metadata">

**Author:** ![foobar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@foobar](https://www.ruby-forum.com/u/foobar)\
**Post date:** [January 30, 2008, 1:43am UTC](https://www.ruby-forum.com/t/problem-with-virtual-page-s-and-find-by-url/127503/5 "2008-01-30T01:43:18Z")

</div>

On Jan 29, 2008 10:26 PM, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If this is unclear, write me back and I’ll give more detail.  
> OK - got it. Thanks Sean.
