views:

32

answers:

2

Hello!

I have a photo_album which has several photos, IDs: 4,9,10,11,31 -- all for photo_album:3

I have the following for my DEF SHOW in my Photos controller:

@photo = @photoalbum.photos.order("created_at ASC").paginate :page => params[:page], :per_page => 1

Problem is, if the URL is: /photo_albums/3/photos/10 The code above starts at record 4, when I want it to start the will paginate at 10, like in the URL.

Anyone know how to set the starting ID in will_paginate?

Thanks

UPDATE

Also, noticing the PAGINATION links are rendering link this:

/photos/10?page=2&photo_album_id=3

When they should be rendering like:

/photo_albums/3/photos/?page=2

Does that sound right?

Here's the model info:

class PhotoAlbum < ActiveRecord::Base
    has_many :photos, :dependent => :destroy

class Photo < ActiveRecord::Base
    belongs_to :photo_album

Update 2 - Desired User Flow...

  • #1 - User sees a list of photo albums
  • #2, Users clicks a Photo Albums
  • #3 User see a list of photos in the album
  • #4 use views the clicked photo BUT has will_paginate showing and defaulted to the page number of the photo the user clicked

Update 3 - Controllers based on the flow

  • Photo Albums per Project - PhotoAlbum Index
  • Photos Per Photo Album - PhotoAlbum Show
  • Photo Show - Photo Show

Does this sound right? Still at it, no luck yet. Appreciate any advice.

A: 

I'm not sure why you're using a Show method. To use Pagination, you should be on the Index method of the PhotoAlbum controller.

so your links should be

link_to('Show Photos', photo_album_path(@photo_album, page=>params[:page])

If you want to Show only one photo, cool, but don't use pagination.

link_to('Photo), photo_album_photo_path(@photo_album, @photo))
Jesse Wolgamott
@Jesse, thanks this is interesting. Just to make sure I'm being clear about the Flow. #1 - User sees a list of photo albums, #2, Users clicks a Photo Albums, #3 User see a list of photos in the album, #4 use views the clicked photo BUT has will_pagine showing and defaulted to the page number of the photo the user clicked.
TheExit
Given the above, where would your Link_to be? I need help creating the photos links for #3?
TheExit
The PhotoAlbum INDEX shows a list of all the user's photo albums, not the list of photos in the album.
TheExit
What and where does "page=>params[:page]" come from? Trying to understand how I use that to set the starting photo in the album?
TheExit
+1  A: 

As Jesse pointed out, you should be using index to display a list of photos, not show, even though you are only showing one photo per page.

Using the finder in your controller gives you a WillPaginate object in your view:

def index
  @photos = @photoalbum.photos.order("created_at ASC").paginate :page => params[:page],
    per_page => 1
end

In the view, will_paginate knows about the has_many relationship, so it will include the params for the parent photo_album

<%= will_paginate(@photos) %>

renders the next link as:

/photos?page=2&photo_album_id=3

You might have expected /photo_albums/3/photos?page=2, but will_paginate does not use the nested resource path. That's OK, because the URL ends up in the same place, i.e. { :controller => :photos, :action => :index, :photo_album_id => 3 }

You can pass in the parent explicitly to override if necessary:

<%= will_paginate(@photos,:params => {:photo_album_id => 3}) %>

More info in the will_paginate docs

EDIT

OK Thanks but what about showing a "STARTING RECORD" meaning if an album has 8 photos, and the user clicks to see photo 4, how to I show that photo in the right pagination order, using the INDEX ?

You could add the page number to the link in the album view. Maybe something like this:

<% @photos.each_with_index do |photo, index| do %>
  <%= link_to "View photo",
    photo_album_photos_path(@photo_album, :page => index) %>
<% end %>
zetetic
OK Thanks but what about showing a "STARTING RECORD" meaning if an album has 8 photos, and the user clicks to see photo 4, how to I show that photo in the right pagination order, using the INDEX ?
TheExit
Also, how should I be linking to the photo view page? I have "photo_path(photo)" currently but that goes to the show page, and it sounds like your saying I need to use INDEX?
TheExit
So the underlining issues is that I have a PhotoAlbum Index, and it can't be used to display a list of photoalbums, and render the photos. Two separate views? Right
TheExit
undefined method `photo_album_photo_path' for #<#<Class:0x107cd04d0>:0x107cce108>
TheExit
Damn, you're quick :) Try `photo_album_photos_path(@photo_album)`, or pass in the photoalbum ID.
zetetic
thanks, I did that, but remember that's on the page that is showing all the thumbnails of photos contained in a photoalbum. SO that link is the same for every photo which is bad. It should link to the page where the user can view the photo but it should contain a photoID so willpaginate knows which photo to display? right?
TheExit
oh it worked! Also the index was a great idea, but index starts at 0, so I had to do index+1
TheExit
Thank you so much. I spent the last 5 hours on this one!
TheExit
The links won't be the same -- each link goes to a different page. The URL should not include the photo id -- it will pull the correct photo object out of the paginated collection based on the requested page number. You could easily extend this later to show more than one photo in the paginated view, if necessary, by increasing `:per_page`. At the risk of complicating matters, you *could* show one photo by using a `show` action, finding the photo by the ID, and (somehow) generating the prev/next links, but using `index` action will be easier because it fits within the will_paginate paradigm.
zetetic