使用 Carrierwave 和 Kaminari 进行图像分页
我正在尝试使用 Kaminari gem 对一系列照片进行分页。这些图像正在 Dog 模型的 show 动作中显示,并且照片已使用 CarrierWave 上传到名为 DogPhoto 的单独模型。 DogPhoto属于Dog,Dog有很多DogPhoto。
Dog 控制器的显示操作如下所示(@dog 在过滤器之前加载):
def show
@dog_photos = @dog.dog_photos.page(params[:page]).per(1)
end
并且显示视图如下所示:
<div id="dog_photos">
<% if @dog_photos.count > 0 %>
<% @dog_photos.each do |dog_photo| %>
<%= image_tag dog_photo.photo_url %>
<% end %>
<%= paginate @dog_photos %>
<% else %>
<%= image_tag("dog-with-no-photo.png", :border => false) %>
<% end %>
</div>
加载页面时,将显示正确数量的照片的分页链接(每张照片一页) 。但是,仅显示第一张照片。当用户单击“2”时,它会显示dog-with-no-photo.png 后备图像。
I'm attempting to paginate a series of photos using the Kaminari gem. The images are being displayed in the Dog model's show action, and the photos have been uploaded using CarrierWave to a separate model called DogPhoto. DogPhoto belongs to Dog, and Dog has many DogPhotos.
The Dog controller's show action looks like this (@dog is loaded in a before filter):
def show
@dog_photos = @dog.dog_photos.page(params[:page]).per(1)
end
And the show view looks like this:
<div id="dog_photos">
<% if @dog_photos.count > 0 %>
<% @dog_photos.each do |dog_photo| %>
<%= image_tag dog_photo.photo_url %>
<% end %>
<%= paginate @dog_photos %>
<% else %>
<%= image_tag("dog-with-no-photo.png", :border => false) %>
<% end %>
</div>
When the page is loaded, the pagination links are displayed for the correct number of photos (one page per photo). However, only the first photo is displayed. When the user clicks on "2" it instead shows the dog-with-no-photo.png fallback image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该删除
show
操作中的.per(1)
代码,因为它将每页的图片数量限制为一张。You should get rid of the
.per(1)
code in yourshow
action since it limits the number of pictures per page to only one.