2014-09-30 96 views
0

我正在創建圖片庫網站,並且存在一些問題。我嘗試搜索,但很難,因爲我無法真正描述這個問題。Ruby on rails使用回形針的圖片庫

我想在我的索引頁上有縮略圖圖像,但我無法使它工作。

爲什麼裏面/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指數 @galleries = Gallery.all.order(created_at:降序) 端

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

路線: 資源:畫廊 資源:照片

回答

1

@rubynuby :試試這個:

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

A是我觀察到的表,您的問題是:在

型號

/gallery.rb

class Gallery 
    has_many :photos 
end 
在模型

/photo.rb

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

end 

所以按照您的要求,您需要調用image上照片,即photo.image.url(<style>)

+0

像一個魅力工作 – rubynuby 2014-10-01 18:03:32