# Re: Next and Previous Articles

**URL:** <https://www.ruby-forum.com/t/re-next-and-previous-articles/67794>\
**Category:** Radiant CMS\
**Created:** [September 6, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794 "2006-09-06T21:26:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 6, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/1 "2006-09-06T21:26:55Z")

</div>

Keith B. wrote:

> Not as elegant as John’s, but for some reason current.siblings  
> returns an empty array.

FWIW, the problem wasn’t that current.siblings was returning null.  
Rather, the problem was that current.siblings returns a list that  
doesn’t include the current page [1], so siblings.index(current) was  
returning null. Calling “self\_and\_siblings” instead of “siblings”  
works fine. Here’s the corrected version:

class Behavior::Base

define\_tags do

```
 tag "next" do |tag|
   current = tag.locals.page
   by = tag.attr['by'] || 'title'
   siblings = current.self_and_siblings.sort_by { |page|

```

page.attributes[by] }  
index = siblings.index(current)  
next\_page = siblings[index + 1]  
if next\_page  
tag.locals.page = next\_page  
tag.expand  
end  
end

```
 tag "previous" do |tag|
   current = tag.locals.page
   by = tag.attr['by'] || 'title'
   siblings = current.self_and_siblings.sort_by { |page|

```

page.attributes[by] }  
index = siblings.index(current)  
previous = siblings[index - 1]  
if previous  
tag.locals.page = previous  
tag.expand  
end  
end

end

end

- Sean

[1] [Peak Obsession](http://ar.rubyonrails.com/classes/ActiveRecord/Acts/Tree/)  
ClassMethods.html

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 11, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/2 "2006-09-11T22:03:04Z")

</div>

On 06/09/06, Sean S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s the corrected version:

If I want to use these behaviours, how do I go about that? 🙂

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 11, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/3 "2006-09-11T22:43:10Z")

</div>

> If I want to use these behaviours, how do I go about that? 🙂

Oh, and if you were also wondering how you would actually use the  
tags once you’ve got the behavior installed, here’s an example:

\<r:previous by=“published\_at”\>\<r:link/\>\</r:previous\>  
\<r:next by=“published\_at”\>\<r:link/\>\</r:next\>

- Sean

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 11, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/4 "2006-09-11T22:39:08Z")

</div>

> If I want to use these behaviours, how do I go about that? 🙂

Create a file named “00\_next\_previous\_behavior.rb”, paste in the  
code, and save it to your radiant/app/behaviors directory. Then  
restart the app.

HTH,

- Sean

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 12, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/5 "2006-09-12T13:46:45Z")

</div>

On 11/09/06, Sean S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hi Sean,

Thanks for the quick response! 🙂

> > If I want to use these behaviours, how do I go about that? 🙂
> 
> Create a file named “00\_next\_previous\_behavior.rb”, paste in the  
> code, and save it to your radiant/app/behaviors directory. Then  
> restart the app.

There was no radiant/app directory so I’ve created it and its  
behaviors child. I now get these errors:

undefined tag `previous' undefined tag `next’

Could this work if it was packaged as a plugin?

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 12, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/6 "2006-09-12T15:08:57Z")

</div>

On 12/09/06, Sean S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > There was no radiant/app directory so I’ve created it and its  
> > behaviors child. I now get these errors:
> 
> Sorry, Dave, I should have been more clear. In your Radiant  
> installation (wherever that is), find the app directory, in which  
> there should be a behaviors directory. For example, if you installed  
> radiant in /home/dave/foo/radiant-0.5.2, you should put the  
> 00\_next\_previous\_behavior.rb in /home/dave/foo/radiant-0.5.2/app/  
> behaviors/.

Ah, another issue caused by using the gem installation then.

I’ll look into using the SVN version I think 🙂

Thanks again!

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 12, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/re-next-and-previous-articles/67794/7 "2006-09-12T14:40:54Z")

</div>

> There was no radiant/app directory so I’ve created it and its  
> behaviors child. I now get these errors:

Sorry, Dave, I should have been more clear. In your Radiant  
installation (wherever that is), find the app directory, in which  
there should be a behaviors directory. For example, if you installed  
radiant in /home/dave/foo/radiant-0.5.2, you should put the  
00\_next\_previous\_behavior.rb in /home/dave/foo/radiant-0.5.2/app/  
behaviors/.

HTH,

- Sean
