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-%>
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?
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?
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.
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.