开发者

Will_Paginate --- Set the Starting record?

开发者 https://www.devze.com 2023-01-22 23:18 出处:网络
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:

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.


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


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消