如何以这种方式对太阳黑子进行分页?
我有以下模型关联:
class Product < ActiveRecord::Base
has_many :prices
class Price < ActiveRecord::Base
belongs_to :product
这是我的控制器
class SearchController < ApplicationController
# Using Sunspot here.
def index
@search = Product.search do
fulltext params[:search]
paginate(:per_page => 5, :page => params[:page])
end
@products = @search.results
end
end
,然后是我的视图:
<%= will_paginate @products %>
<% @products.each do |product| %>
<%= product.name %>
<% prices.each do |price| %>
<%= price.price %>
<% end %>
<% end %>
假设我有 10 个产品,每个产品有 20 个不同的价格。我的目标是每页显示 5 个产品,但如果页面上的价格数量一次超过 25 个,则价格会转到下一页。
这是我想要它做什么的两个不同的例子:
Product 1, 2, 3, 4 + 5 Prices each = same page
Product 5 + 6 Prices = this Product with Prices on next page
或者
Product 1 + 23 prices = same page
Product 2 + 20 prices = next page with Product and prices
我怎样才能实现这个?
I have the following model associations:
class Product < ActiveRecord::Base
has_many :prices
class Price < ActiveRecord::Base
belongs_to :product
Here is my controller
class SearchController < ApplicationController
# Using Sunspot here.
def index
@search = Product.search do
fulltext params[:search]
paginate(:per_page => 5, :page => params[:page])
end
@products = @search.results
end
end
and then my view:
<%= will_paginate @products %>
<% @products.each do |product| %>
<%= product.name %>
<% prices.each do |price| %>
<%= price.price %>
<% end %>
<% end %>
Lets say I have 10 Products that each have 20 different Prices. My goal is to show 5 Products per page but have the Prices run over to the next page if the amount of Prices on a page exceeds a maximum of 25 at a time.
Here is two different examples of what I want it to do:
Product 1, 2, 3, 4 + 5 Prices each = same page
Product 5 + 6 Prices = this Product with Prices on next page
Or
Product 1 + 23 prices = same page
Product 2 + 20 prices = next page with Product and prices
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到的第一件事是查询价格模型:
并且为了让您的视图更容易:
prices.map! { |p| p.产品}
The first thing that comes to my mind is to query over the Price model:
And to make things easier for your views:
prices.map! { |p| p.product }