新手问题 多个模型嵌套,will_paginate 怎么分页

yeyong14 · 2013年12月30日 · 最后由 yeyong14 回复于 2013年12月30日 · 2073 次阅读

三个模型,category, tag, product, 模型的关系

#category
class Category < ActiveRecord::Base
has_many tags
end

#tag
class Tag < AcriveRecord::Base
has_many :products
belongs_to category
end

#product
class Product < AcriveRecord::Base
belongs_to tag
end

现在的需求是在categoryproducts进行分页

#categories/show
#
#我现在是这么做的, 但是没什么分页效果,
def show
@category = Category.find(params[:id])
@tag = @category.tags.build
@products = @tag.products.paginate(page: params[:page],per_page: 10)
end

#category/view
<div class="cate-show">
<% @category.tags.each do |tag| %>
  <% tag.products.each do |product| %>
   <ul class="thumbnail">
     <li> <%= image_tag(product.image_url) %></li>
   </ul>
    <% end %>
<% end %>
</div>
<%= will_paginate @products %>

这样没有分页效果,如果对@tags = @category.tags.paginate(page: params[:page],per_page: 2) 只对tag进行分页,没有对products进行分页

tag.products -> @products 我收回……,LZ 代码略奇怪,@products = @tag.products.paginate(page: params[:page],per_page: 10) 这句中的 @tag 是空的你没发现么?

@lufeihaidao 不是很了解。怎么写的

@lufeihaidao

@tag = @category.tags.find(params[:id])

Couldn't find Tag with id=1 [WHEREtags.category_id= 1]

需要 登录 后方可回复, 如果你还没有账号请 注册新账号