Item's page and will_paginate

后端 未结 3 1369
难免孤独
难免孤独 2021-01-12 12:18

I have some photos that are split on successive pages through will_paginate plugin. When you open a photo and then return to all photos using a link, you always return to th

相关标签:
3条回答
  • 2021-01-12 12:48

    The page number is highly dependent on the results of your search. Your search might have 1, 10, or 100 pages depending on the result set and the number of items per page.

    Any link to "show all photos" could contain the search and pagination information, using the GET parameters as you've described. Or store and retrieve via a cookie so that the search results persist until the users clears or selects a new search.

    0 讨论(0)
  • 2021-01-12 12:52
    page = (number_of_records_before_RECORD / number_of_records_per_page) + 1
    

    In other words. If your photo has ID 40 and there are 25 records before (assuming some records have been deleted), with 20 records per page:

    page = (25 / 20) + 1 = 2
    

    You can count the number of records before the selected record using Model.count(:conditions => ['id < ?', record.id], :order => 'id'). The right query depends on which sorting filter you apply to that table when listing all objects.

    0 讨论(0)
  • 2021-01-12 13:04

    Here's another solution: will_paginate just uses query string parameters 'search_field' and 'page'; you can extract these from the Rails params hash. If you keep track of those using session state, you can reapply them in your controller code, when needed.

    Exactly how you manage that will depend on your application. In the app I'm working on, the flow is such that I can distinguish between a general context and a member context. The user enters the member context from the member#index page. So I just set session[:member_context] on entry to the member context; e.g. in members#edit. Then, in the members#index, I have the following code:

    if session[:member_context]
      @search_field = session[:search_field]
      @page = session[:page]
      # toggle out of member context
      session[:member_context] = nil
    else
      @search_field = params[:search_field]
      @page = params[:page]
      # record lastest search in case the user subsequently enters the member context
      session[:search_field] = @search_field
      session[:page] = @page
    end
    @members = Member.where(<use @search_field>).page(@page)
    # and render ...
    

    This works great in my application.

    0 讨论(0)
提交回复
热议问题