Rails分页不会为搜索结果的第二页加载内容

时间:2016-11-02 01:39:21

标签: ruby-on-rails search controller pagination will-paginate

我有一个方法来获取搜索结果并显示它正常工作。但是,每当我尝试进入第二页时,都会说没有内容的错误。

这是我的控制器代码:

class BuyersController < UsersController

def show
end

def search
end

def search_products
    search_name = params[:search][:name]
    search_category = params[:search][:category_id].to_i
    search_location_id = params[:search][:location_id].to_i
    search_highest_bid = params[:search][:highest_bid].to_f
    @matching_products = ProductsUnderBid.search_products_under_bid(name: search_name, category_id: search_category, 
                                                                    location_id: search_location_id, highest_bid: search_highest_bid)
    @matching_products = @matching_products.paginate(page: params[:page], per_page: 1)
end
end

2 个答案:

答案 0 :(得分:0)

尝试删除per_page:1,因此您必须输入如下代码:

@matching_products = @matching_products.paginate(page: params[:page])

答案 1 :(得分:0)

我改变了下面的方法并且它有效!正如您所看到的,我在search_products方法中将变量类型变为全局,以便我可以从show方法访问它。我还必须在app / view / controller_name /文件夹下添加show.html.erb。希望它能帮助别人。

def show
    @matching_products = ProductsUnderBid.search_products_under_bid(name: $search_name, category_id: $search_category, 
                                                                            location_id: $search_location_id, highest_bid: $search_highest_bid)
    @matching_products = @matching_products.paginate(:page => params[:page],:per_page => 5)
            get_already_placed_bids(@matching_products)
end

def search_products
    $search_name = params[:search][:name]
    $search_category = params[:search][:category_id].to_i
    $search_location_id = params[:search][:location_id].to_i
    $search_highest_bid = params[:search][:highest_bid].to_f
    @matching_products = ProductsUnderBid.search_products_under_bid(name: $search_name, category_id: $search_category, 
                                                                    location_id: $search_location_id, highest_bid: $search_highest_bid)
    @matching_products = @matching_products.paginate(:page => params[:page],:per_page => 5)
    get_already_placed_bids(@matching_products)
end