Pagination vs. Tags for viewing featured items


#1

Goal:

  • Label certain “items” in the database as “featured.”
  • View the “featured items” on a list page
  • On the list page, each “featured item” links to a item show page.
  • On each “featured item” show page, there are links to show the
    previous or next “featured item”
  • The show “previous featured item” and show “next featured item”
    links should be automagicly generated in the layout.

There are probably a number of ways to accomplish this, but my big
n00b is getting in the way.

First Guess: Pagination Module
My first guess was that I could use something within the Pagination
module, but so far Pagination appears to be limited to taking all the
“items” in the database, and showing them in order by id in sets of a
X number of items.

I don’t see a pagination option for labeling certain items as
“featured” and then listing only those items, nor a way of placing
links in the view so as to store the position within the “featured
item list” as the browser moves from “featured item” page to “featured
item” page.

Second Guess: Acts_as_Taggable Plugin
Second, I tried using the act_as_taggable plugin (not gem) and then
the act_as_taggable_on_steroids plugin. This pretty much fell apart
from the start since I spent most of the day hunting down missing
setup instructions.

The wiki HowTo says one thing, the on_steroids site says another.
Neither seems to include all the information necessary to set the
plugin up. I tried following the “Rails Recipes” book (The Pragmatic
Programmer has a free PDF download of Chapter 19) but it is
embellished with trips to the “ruby script/console”, layered partials,
and ajax – all of which really confused me.

If I go this route, it seems like I need a join table, but not a
table1_table2 join table, but instead a special join table named
“taggings”.

Anyway, I am starting to whine more than ask for help, so … help …
anyone?

My best guess at this point is:

  1. Create a tags table with an “id” field as primary key and “name”
    field
  2. Create a tag named “featured”
  3. ?? Use a join table between tags_items to tag specific “items” as
    “featured”
  4. ?? Controller: Some how assemble an array of only the “featured
    items”
  5. ?? View: Index page renders each featured_item in @featured_items
  6. ?? View: Item page somehow set the previous/next links to
    current_array_item ±1
  7. Weep for joy that someone helped me get it to work.

#2

If I got it right, you can use pagination (pagination helper is not
necessary)

Controller
@featured = Item.find(:all, :conditions => “type=‘featured’”, :limit =>
1,
:offset => #{page})
@total_featured = Item.count(“id”, “type=‘featured’”)

View
Previous
Next

You need some checks to for the previous/next links.

G.


#3

Thanks for the suggestion Bandito. Your code got me started thinking
in the right direction. I’m not sure I totally grasped what you
proposed, but it did help me get the first chunk of my goal done.

I walked away from the act_as_taggable / act_as_taggable_on_steroids
plugins. I don’t need a full tag system, so why implement one.

On Feb 20, 3:20 pm, "Tag_Sadness" <removed_email_address@domain.invalid> wrote:
    > Goal:
    > - Label certain "items" in the database as "featured."
    > - View the "featured items" on a list page
    > - On the list page, each "featured item" links to a item

show page.

Instead I add a column named “featured” to my “items” databasea nd
assigned a few items to have a “featured” value of 1 (e.g. 1 = yes, 0
= no).

ALTER TABLE items ADD featured int(1) NOT NULL

UPDATE items SET featured = 1 WHERE id = 5

Then in my controller:

def list
    @items = Item.find(:all)

    featured_items = []

    for item in @items
        if item.featured == 1
            featured_items << item
        end
    end

    @features = featured_items
end

Then in my “list” view:

<div class="item-thumbnails">
    <% for feature in @features -%>
        <% for image in feature.images -%>
            <% if image.position == 5 -%>
                <%= link_to( image_tag( image.file_name, :title =>

feature.title, :alt => image.alt ),
{:action =>
‘show’, :id => feature.id} ) -%>
<% end-%>
<% end-%>
<% end-%>

Obviously I had a pre-existing relationship between items and images
that persists when an item is inside the @features array. There is
probably a way to move some of this code back to the controller, but I
haven’t gotten anything to work quite yet. I wish I had a better
programming foundation. It will come in time with practice I guess?

So now with that working, my next set of goals are:

On Feb 20, 3:20 pm, "Tag_Sadness" <removed_email_address@domain.invalid> wrote:
    > - On each "featured item" show page,
        there are links to show the previous or next "featured

item"

    > - The show "previous featured item" and show "next featured

item"
links should be automagicly generated in the layout.

This is where I think Bandito has filled in some blanks. I didn’t have
the @features array populated with data before.

Now I think I need to use the “limit and offset” to move through the
@features array one at a time using the simple math in the anchor
element bandito provided.

Wish me luck. More advise is always appreciated. And again, thank you
Bandito.