简体   繁体   中英

Rails4: Will_paginate gem issues

Trying to implement the Will_paginate gem into my rails app, getting error undefined method `paginate' for #. Using gem 'will_paginate', '~> 3.0'

have added the gem to my gemfile and search controller.rb file

class SearchController < ApplicationController
  def Home
    access_token = ENV["ACCESS_TOKEN"]
    client = Instagram.client(access_token: access_token)
    default_search = client.tag_search('pizza')

    if params[:q]
      search_query = client.tag_search(params[:q])
      @tag = search_query.present? ? search_query : default_search
    else
      @tag = default_search
    end

    @tag = @tag.first.name
    @results = client.tag_recent_media(@tag)**.paginate(:page => params[:page], :per_page => 4)**
  end

  def about
  end
 end

and added to the home page so it displays the page numbers

<form action="?" method="get">
            <input type="text" name="q" placeholder="Search Instagram photos..." autofocus/>
        </form>

        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Welcome //
                    <small>It's Nice to Meet You!</small>
                </h1>
     </div>

        <div class="row">
        <% @results.each do |instagram| %>
            <div class="col-md-3">
            <%= image_tag instagram.images.low_resolution.url %>
            </div>
            <% end %>
        </div>
        <%= will_paginate @results %>
        <br/>

will_paginate only knows how to paginate ActiveRecord queries (and optionally arrays) out of the box.

It doesn't know how to paginate arbitrary apis though. You can use WillPaginate::Collection to wrap any data, however it doesn't look like the Instagram api fits very well with this: will paginate assumes that you can skip to an arbitrary offset which you can't with the api. The Instagram api doesn't let you do this - you have to specify the max id and you dont know what that will be other than for the next page of results.

In short it doesn't really look like you can sensibly use will_paginate with the Instagram api

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM