Tag: 分页

将will_paginate与多个模型一起使用(Rails)

很确定我在这里遗漏了一些非常简单的东西: 我正在尝试显示一系列包含两个不同模型实例的页面 – 配置文件和组。 我需要按名称属性排序。 我可以为每个模型选择所有实例,然后对它们进行排序和分页,但这感觉很邋and且效率低下。 我正在使用mislav-will_paginate,并想知道是否有更好的方法来实现这一目标? 就像是: [Profile, Group].paginate(…) 会是理想的!

如何使用“github_api”gem从GitHub API v3获得100多个结果?

我正在使用GitHub API Gem并尝试获取有关贡献者的添加,删除和提交计数的统计信息 。 问题是我只得到100个结果,无法访问其他页面。 这似乎是非常常见的问题,但我找不到答案。 例如,让我们来看看rails / rails repo。 有1 990个贡献者: repo = Github::Repos.new user: ‘rails’, repo: ‘rails’ repo.stats.contributors 我得到的是前100个结果。 我试图查询链接头中包含的分页信息。 我在rails console中的输出: irb(main):001:0> repo = Github::Repos.new => # irb(main):002:0> res = repo.stats.contributors user: ‘rails’, repo: ‘rails’ => # irb(main):003:0> res.links => # 没有。 传递auto_pagination选项不会改变我的任何内容。 我错过了什么?

集成测试:`assert_select’a ‘`对于分页页面失败

我有很多集成测试,看起来像: first_page_of_users = User.page(1) # Using kaminari for pagination. first_page_of_users.each do |user| assert_select ‘a[href=?]’, user_path(user) end 这些测试失败并显示错误消息,例如: Expected at least 1 element matching “a[href=”/users/1″]”, found 0.. 我将puts @response.body添加到测试中,以查看它响应的代码。 对于响应正文中包含的所有用户,它具有正确的href,例如href=”/users/75938613″ 。 但是, /users/后面的数字要高得多,并且“users1”确实不存在(不仅仅是url不存在,而且整个记录不在该页面上)。 如果我确保只有几个固定装置,以便不适用分页,则测试通过。 这让我觉得好像有了分页, first_page_of_users = User.page(1)应用的顺序与first_page_of_users.each do |user|的顺序不同first_page_of_users.each do |user| assert_select ‘a[href=?]’, user_path(user)期望… 可能导致此错误的原因是什么? 它适用于我的应用程序的不同页面和模型。 更新:对于另一个模型,即使我尝试 Organization.all.each do |org| puts @response.body assert_select ‘a[href=?]’, organization_path(org) […]

Geocoder Gem:如何在两种不同型号之间使用?

我正在使用Geocoder和Will Paginategem根据用户所在位置和孩子妈妈的地址之间的距离来对结果进行排序。 我的模型是这样的: class User has_many :kids geocoded_by :address end class Kid belongs_to :mom belongs_to :dad end class Mom has_many :kids has_many :dads, through: :kids geocoded_by :address end class Dad has_many :kids has_many :moms, through: :kids end 现在,我正在尝试使用正确的范围,我将使用Users.address并将其与Kids.moms.addresses进行比较 def show @dad = Dad.find(params[:id]) @user_location = current_user.address @kids = @dad.kids.near(@user_location, 100).paginate(page: params[:page], per_page: 25).order(created_at: :desc) end […]

如何让paginate工作而不是重定向到’public / index.html’?

我的主页使用’public / index.html’,它覆盖了我在这里阅读的所有内容。 因此,我已将所有已登录的用户重定向到http://localhost:3000/home ,我将其定义为get ‘/home’ => ‘static_pages#home’, :as => :home in my路线 我面临的问题是,在登录用户(/ home)的主页上,有分页。 当我尝试点击第2页http://localhost:3000/?page=2 ,它会被发送到public / index.html。 我尝试了链接http://localhost:3000/home/?page=2作为测试,但它给了我一个空白的分页。 没有显示任何项目。 我怎样才能解决这个问题? 这是控制器 class StaticPagesController 1, :per_page => 10) else redirect_to root_path end end 然后在我看来 这是_item.html.erb部分 <li id="”> posted made a comment shared this 150, :omission=>’ …(continued)’) %>

Google自定义搜索API与分页

我有这种方法将来自Google自定义搜索API的10个结果的链接放入一个数组中: require ‘json’ require ‘open-uri’ def create search = params[:search][:search] base_url = “https://www.googleapis.com/customsearch/v1?” stream = open(“#{base_url}key=XXXXXXXXXXXXX&cx=XXXXXXXXXX&q=#{search}&start=#{i}&alt=json”) raise ‘web service error’ if (stream.status.first != ‘200’) result = JSON.parse(stream.read) @new = [] result[‘items’].each do |r| @new << r['link'] end end 和我的观点: 我无法弄清楚如何添加分页,以便在第二页上返回接下来的10个结果。 我正在使用Kaminarigem进行分页。 我希望当用户点击指向其他网页的链接时,我会从Google的API中获取接下来的10个结果。 您可以使用API​​的start参数执行此操作,该参数指定要开始的第一个结果,我将其作为i传递。 我在考虑做这样的事情: i = (params[:page] – 1) * 10 + 1 其中params[:page]是当前的页码,但由于某种原因它是未定义的。 另外,我不确定如何为不是AR对象的数组设置分页,以及我认为会有什么。 […]

Soundcloud API支持分页?

我如何使用“linked_pa​​rtitioning = 1”来调用响应中的“next_href”? 我在响应中看到next_href,但我不知道下一步该做什么? :\请帮忙提前谢谢:)

使用will_paginate而不使用:total_entries可以改善冗长的查询

我有一个当前的will_paginate实现,它使用paginate_by_sql方法来构建要分页的集合。 我们有一个针对total_entries的自定义查询,这个查询非常复杂并且会给我们的数据库带来很大的负担。 因此,我们希望完全从分页中删除total_entries。 换句话说,我们只是喜欢“下一个 – 上一个”按钮,而不是“前一个[2] 3 4 5下一个”的典型分页显示。 但我们需要了解一些事情。 我们是否显示上一个链接? 只有当记录存在于当前选择中显示的记录之前时,才会发生这种情况 我们是否显示下一个链接? 如果正在显示集合中的最后一条记录,则不会显示此信息 来自文档 如果您不提供:total_entries,则会自动生成对行计数的查询。 如果您遇到此生成的SQL的问题,您可能希望在应用程序中手动执行计数。 所以最终理想情况如下。 删除total_entries计数,因为它会导致数据库负载过大 使用半分页显示50个记录,仅使用下一个/上一个按钮进行导航,而不需要显示所有可用的页码 仅相应显示下一个按钮和上一个按钮 有没有人处理类似的问题或对决议有想法?

没有gem的Paginate Next,Previous,Name.order(:id).limit(10).offset(0)的按钮

我一直在寻找解决我问题的方法一个多星期了。 由于没有下一个/上一个function而没有时间,我有一个我失去10分的任务。 我仍然想弄清楚这一点。 我创建了一个简短的单页网站,其中包含rails generate scaffold Ripple name:string message:text url:string ,显示最近发布的10个post的索引(名称,消息,created_on,link_to“show”)。 我仍然需要创建下一个,之前的,最新的,最旧的链接,以显示下一个10,前10 ….结果。 我的代码。 app\controllers\ripple_controller.rb class RipplesController < ApplicationController before_action :set_ripple, only: [:show, :update] before_action :restrict_destroy_edit, only: [:edit, :destroy] before_filter :set_page helper_method :link_name_to_url, :next_button, :previous_button, :newest_button, :oldest_button, :is_next_page_available, :is_previous_page_available RIPPLES_PER_PAGE = 10 def index @ripples = Ripple.order(:id).limit(RIPPLES_PER_PAGE).offset(@page * RIPPLES_PER_PAGE) end #All my show, new, destroy, […]

Ruby on rails – 搜索结果的分页

我有2个模型,post和位置,其中位置has_manypost和postbelongs_to位置。 搜索工作正常,除了total_entries之外,分页也很好。 它在结果中显示了10个以上的条目 查看search.html: ‘get’ do %> nil %> 控制器post_controller.rb: def search title = params[:title] company = params[:company] location_id = params[:location_id] @posts = Post.search(title, company, location_id) end 模型Post.rb def self.search(title, company, location_id) if location_id.present? paginate :conditions => [‘title LIKE ? AND company LIKE ? AND location_id = ?’, “%#{title}%”, “%#{company}%”, location_id], :per_page => 20, :order […]