2011-01-06 96 views
0

如果在Rails中工作,定義生成許多圖像標籤的輔助函數的最佳方法是什麼?然後這個函數會被一個.erb文件調用,產生一個視圖。Rails中的圖像標籤生成

換句話說,像

def build_view; image_tag("seg-433.png", :alt => "Shit don't work", :class => "round"); end 

但返回許多標籤。

隨意提出一個更習慣的方法,我剛開始騎着Rails列車,就像昨天一樣。

回答

1

如果你有,你可以創建這樣一個輔助的圖像模式:

/app/helpers/my_controller_helper.rb

module MyControllerHelper 
    def bunch_of_image_tags 
    images = [] 
    Image.all.each do |image| 
     images << image_tag(image.path, :alt => image.alt, :class => image.class) 
    end 
    images.join("<br/>") 
    end 
end 

你也可以從文件系統得到的文件列表,但我不確定在這種情況下你會爲alt標籤使用什麼。另請參閱paper_clip - https://github.com/thoughtbot/paperclip

+0

images image.alt,:class => image.class) – Shane 2011-01-06 18:38:23

0

您可以呈現包含圖片標記所需所有內容的部分集合。

render :partial => "image", :collection => @images 

部分「圖像」是包含圖像標籤的圖像。 More at api.rubyonrails.org