Simple Navigation of Array

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

This solution is working for part of the goal:

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


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

<% end-%>

Then I get stuck:

    def show
        ? ? ? ?

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

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?

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?

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

This is what I am trying as per your suggestion.

    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)

    <%= link_to('Previous', { :page =>

@feature_pages.current.previous }) -%>
<%= link_to(‘Next’, { :page => }) -

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?

On 3/5/07, Rian [email protected] 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? %>



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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs