# Simple Navigation of Array

**URL:** <https://www.ruby-forum.com/t/simple-navigation-of-array/87375>\
**Category:** Rails\
**Created:** [March 2, 2007, 2:31am UTC](https://www.ruby-forum.com/t/simple-navigation-of-array/87375 "2007-03-02T02:31:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Rian](https://www.ruby-forum.com/u/Rian)\
**Post date:** [March 2, 2007, 2:31am UTC](https://www.ruby-forum.com/t/simple-navigation-of-array/87375/1 "2007-03-02T02:31:17Z")

</div>

Goal: Browse a specific set of database records (the array @features)  
as a list. Select an item from list (item[3]) to view. On item page  
(i.e. ./show/3 ) navigate to previous / next item page using generated  
links.

This solution is working for part of the goal:

```
ItemController
    def list
        @features = Item.find(:all, :conditions => "featured = 1")
    end

list.rhtml

```

## \<% for feature in @features -%\> \<%= link\_to(feature.title, {:action =\> ‘show’, :id =\> feature.id} )

%\>  
\<% end-%\>

Then I get stuck:

```
ItemController
    def show
        ? ? ? ?
    end

show.rhtml
    <a href="./items/show/<%= ? ? ? ? %>">Previous</a>
    <a href="./items/show/<%= ? ? ? ? %>">Next</a>

```

Walkthrough:  
browse to: ./items/list/ (displays the array @ features = [3, 5, 12,  
19] )  
click the text link “item\_5\_title” ( @features[1] )  
arrive at: ./items/show/5  
click the text link “next”  
_should_ arrive at: ./items/show/12 ( @features[3] )

How do I tell the previous / next link where to go?

---

<div class="post-metadata">

**Author:** ![Rian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Rian](https://www.ruby-forum.com/u/Rian)\
**Post date:** [March 2, 2007, 3:06am UTC](https://www.ruby-forum.com/t/simple-navigation-of-array/87375/2 "2007-03-02T03:06:21Z")

</div>

I’m pretty new at this too, but it sounds like you just want to  
paginate thru a list with a limit of one item per page. Couldn’t you  
just use the next/previous links that get generated for the list page  
and put them on your show page? Can you paginate @features instead of  
the default paginate :items? I think it uses the id field by default  
anyways, so it should be able to generate it that way. Am I making  
any sense?

---

<div class="post-metadata">

**Author:** ![Rian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Rian](https://www.ruby-forum.com/u/Rian)\
**Post date:** [March 5, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/simple-navigation-of-array/87375/3 "2007-03-05T20:09:37Z")

</div>

Thanks for the suggestions Rick. I am definitely new at this.

This is what I am trying as per your suggestion.

```
ItemController
    def show
        # Load item by id in order to populate page.
        @item = Item.find(params['id'])

        # Create array of all items with "featured = 1"
        @features = Item.find(:all, :conditions => "featured = 1")
        @feature_pages, @features = paginate(:features, :per_page

```

=\> 1)  
end

```
show.rhtml
    <%= link_to('Previous', { :page =>

```

@feature\_pages.current.previous }) -%\>  
\<%= link\_to(‘Next’, { :page =\> @feature\_pages.current.next }) -  
%\>

When I try to load a page ( ./items/show/3 ) in the browser I am  
getting an error.

```
NameError in PortfolioController#try
    uninitialized constant Feature

```

I tired initializing feature as an array ( @features = [] ) n my  
controller to no avail.

Where am I messing this up?

---

<div class="post-metadata">

**Author:** ![Rian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Rian](https://www.ruby-forum.com/u/Rian)\
**Post date:** [March 5, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/simple-navigation-of-array/87375/4 "2007-03-05T22:06:57Z")

</div>

On 3/5/07, Rian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the suggestions Rick. I am definitely new at this.
> 
> This is what I am trying as per your suggestion.

This is what I’m doing in a app I’m working on (I changed my model  
name to “feature” to match yours). I have a “number” field associated  
with each record, and use that for the order. Change to suit.

In the controller’s Show method:

```
@feature = Feature.find(params[:id])
@previous = Feature.find(:first, :order => "number desc",

```

:conditions =\> [“number \< ?”, @feature.number])  
@next = Feature.find(:first, :order =\> “number”, :conditions =\>  
[“number \> ?”, @feature.number])

In the show.rhtml file:

```
<p><%= link_to('Previous', feature_path(@previous)) unless

```

@previous.blank? %\> | \<%= link\_to(‘Next’, feature\_path(@next)) unless  
@next.blank? %\>

HTH,  
jt

---

<div class="post-metadata">

**Author:** ![Rian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Rian](https://www.ruby-forum.com/u/Rian)\
**Post date:** [March 6, 2007, 12:11am UTC](https://www.ruby-forum.com/t/simple-navigation-of-array/87375/5 "2007-03-06T00:11:16Z")

</div>

John,

I think (and may be wrong), but the situation I am working from is  
slightly different from where you started.

My model is not “Feature”. My model is “Item” and has an associated  
field named “featured” that accepts 1 or 0 (yes or no). I am guessing  
your suggested “number” field accepts any number and acts as a  
mechanism to sequence the featured items, basically it is an  
additional unique identifier (although not the primary key). I am  
instead using feature as a concept referring to a subset of Items  
which have the attribute “featured = 1”. I only mention this, because  
I cannot simple “change to suit” since our setup is different.

I am new to ruby, so please excuse my misinterpretation if your code  
does in fact fit my circumstance.

If you would explain a little further how @previous and @next are  
working in the controller it might help me see the direction your  
guiding me towards. Also, in the view code you provided, what does  
“feature\_path” call? Is it calling all the code you provided in the  
controller show method? I am missing what “feature\_path” calls.

I appreciate your help. Thank you.
