Rails Pagination with Kaminari with has_many :through Relationship

喜你入骨 提交于 2019-12-08 05:44:02

问题


I have three relevant models. A User which has_many :photos and belongs_to :dorm, a Dorm which has_many :users and has_many :photos, :through => :users, and a Photo class which belongs_to :users and belongs_to :dorm.

I want to paginate all the photos that are in a dorm with kaminari. I have it in my Gemfile and ran the bundle command.

In my dorms_controller:

@dorm=Dorm.find(params[:id])
@photos=@dorm.photos.page(params[:page]).per(3)

and in my Dorm show view (actually in a partial, _index.html.erm rendered in the show view):

<%= paginate @photos %>

This gives me the error: undefined method 'page' for #<Class:0x107483d68>.

I know why this doesn't work (shouldn't be called on a class), but I don't know how to make it work...


回答1:


hrm, strange. That should work. I actually made a vanilla app with an action you shown above and the following models, but I couldn't reproduce the error.

class Dorm < ActiveRecord::Base
  has_many :users
  has_many :photos, :through => :users
end

class User < ActiveRecord::Base
  belongs_to :dorm
  has_many :photos
end

class Photo < ActiveRecord::Base
  belongs_to :user
end

There should be another root cause in your app code. So, could you track down the problem a bit more? To begin with, does the following code work in your rails console?

@dorm.photos.page(1)



回答2:


  1. is gem 'kaminari' in your Gemfile ?
  2. run bundler after changing your Gemfile


来源:https://stackoverflow.com/questions/6543248/rails-pagination-with-kaminari-with-has-many-through-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!