簡體   English   中英

使用回形針的Ruby on Rails圖片庫

[英]Ruby on rails image gallery using paperclip

我正在創建一個圖片庫網站,但遇到一些問題。 我嘗試搜索,但是很難,因為我無法真正描述此問題。

我想在索引頁面上顯示縮略圖,但無法正常工作。

為什么此代碼位於/views/index.html.erb中

    <% @galleries.each do |gallery| %>
    <%= image_tag gallery.photos(:small).last %>
    <%= link_to gallery.title, gallery_path(gallery) %>

不向我顯示照片,僅顯示應該顯示照片的空白區域。 當我右鍵單擊它們以在新選項卡中打開時,它鏈接到/ images /#.....並給出“無路由”錯誤。

在show.html.erb中,圖片顯示為以下代碼:

      <% @gallery.photos.each do |photo|%>
      <%= image_tag photo.image(:medium) %>
     <% end %>

模特是:畫廊有很多照片,照片屬於畫廊。

圖庫控制器:def index @galleries = Gallery.all.order(created_at::desc)結束

    def show
        @gallery = Gallery.find(params[:id])
    end

路線:資源:圖庫資源:照片

@rubynuby:試試這個:

<% @galleries.each do |gallery| %>
<%= image_tag gallery.photos.last.image.url(:small) %>
<%= link_to gallery.title, gallery_path(gallery) %>

從我觀察到的形式來看,您的問題是:

在models / gallery.rb中

class Gallery
  has_many :photos
end

在models / photo.rb中

class Photo
  belongs_to :gallery
  has_attached_file :image, styles: {
                      small:  '150x150>',
                      medium: '600X600>'
                    }

end

因此,根據您的要求,您需要在照片上調用image ,即photo.image.url(<style>)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM