will_paginate - 协会

 Nicole-sasanh_880 发布于 2023-01-14 15:23
  • php
  • 我正在尝试对一个协会进行分页,但我错过了一些东西.

    这是我需要分页的地方.paginate(:page => params[:page], :per_page => 25).如果我理解正确,我必须在我的控制器中创建一个变量来取城镇?

    <% @alliance.players.each do |p| %>
      <% p.towns.each do |t| %>
        ...
      <% end %>
    <% end %>
    

    我在说什么:

    Alliance ->
      Players ->
        Towns <--
    

    基本上我坚持如何在二级循环中对关联进行分页.

    也许有更好的方法来做到这一点.


    协会:

    class Alliance < ActiveRecord::Base
      # Primary Key
      self.primary_key = 'grepo_id'
    
      # Associations
      has_many :players
    end
    
    
    class Player < ActiveRecord::Base
      # Primary Key
      self.primary_key = 'grepo_id'
    
      # Associations
      has_many :towns
      belongs_to :alliance    
    end
    
    class Town < ActiveRecord::Base
      # Primary Key
      self.primary_key = 'grepo_id'
    
      # Associations
      belongs_to :player, :foreign_key => :player_id
    end
    

    我已经尝试过很多但是没有找到任何解决方案.

    我试图在我的控制器中创建一个变量:

    @alliance_towns = @alliance.players.towns.order("rank ASC").paginate(:page => params[:page], :per_page => 25)
    

    所以我可以打电话,@alliance_towns.each do {}但就此而言,我正在接受

    undefined method `towns' for #
    

    我错过了什么?

    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有