1) A Paginator takes the number of total items, the items per page
and the current page, but knows nothing about the full set of items
and the item contents. So I think you''d have to take your full set
of items being paginated and run through the ranges yourself,
checking to see which page a certain item was on. I haven''t tried it
but maybe something like:
items_per_page = 20
@items = Items.find(:all)
for test_page in 1..(@items.length/items_per_page).ceil
test_offset = (test_page - 1) * items_per_page
test_set = @items[offset..(offset + items_per_page - 1)]
if test_set.include?(item_to_find)
this_page = test_page
break
end
end
2) I have done this two ways:
a) Paginate the set with items_per_page = 1.
b) Find the full set, find the item and then find just the item
before and after the current item.
all_items = Item.find(:all)
@item = all_items.find(:first, :conditions => [''id = ?'',
params[:id]])
this_position = all_items.index(@item)
@prev_item = all_items[this_position - 1] if this_position > 0
@next_item = all_items[this_position + 1] if this_position <
(all_items.count - 1)
HTH,
Kevin Skoglund
On Jun 7, 2006, at 5:07 AM, rails-request@lists.rubyonrails.org wrote:
>
> Hey all,
> I''m building a photo album site (for fun) and have a few
questions.
>
> 1) This site will have a browse section, where you can view the
> photos by
> thumbnail. I want to let the user change how many thumbs they can
> see per
> page. I''ll have a couple links at the bottom of the page letting
> them do
> that. The problem is, if they are in the middle of browsing and
> decide to
> change their page size, the new (different sized) page will
> probably have
> completely different pictures on it from the one before they
> changed the
> size. Even worse, the page number may simply be invalid (if you
> make the
> page size bigger it will need fewer pages to show everything,
> narrowing the
> range of valid page numbers). I know I could fix this by not
> letting the
> user change WHILE they are browsing, but I''m curious:
>
> Is there a decent way to determine what page a specific photo is on
> based on
> the page size? I''m ordering these photos by the date they were
> taken at
> (based on EXIF data).
>
> 2) This is the same flavor of a question I believe, but more
> critical (and
> possibly dumber). I''m also going to have a slideshow, which will
> use Ajax.
> When you click next, it will send you to the next image in the
> album. But
> how do I figure out what the next image is? Is there a way to
> select a
> range of records arounds an exact one? I realize I can use
> sessions to
> store an index or something, but I wasn''t sure if there was a
> better way.
>
> Thanks in advance. I''m sure I''ll get some clever
suggestions.
>
> -Ryan