2010-11-23 66 views
0

我試圖創建一個幫助程序以顯示基於模型屬性的評級星。如何在幫助程序的迭代中顯示多個image_tag

我有以下幾點:

def show_profile_stars(profile) 
    content_tag :span, :class => 'stars' do 
     profile.stars.times do 
     image_tag("stars.gif", :size => "30x30", :class => "gold") 
     end 
    end 
    end 

'明星' 是一個整數字段。

但它不是渲染圖像標籤,而是直接顯示'星號'。

如果我只把image_tag放在沒有迭代塊的地方,它確實會顯示圖像,問題在於迭代。

我想我錯過了接收塊的方法(我還是RoR的新手)。

任何幫助?

謝謝!

回答

1

有兩件事,不會在CSS中使用span上的類來完成(例如,一星,二星等)嗎?

總之,要真正做你想要做什麼嘗試:

stars = [] 
profile.stars.times { stars << image_tag("stars.gif", :size => "30x30", :class => "gold") } 
content_tag :span, stars.join("\n").html_safe, :class => "stars" 
+0

我只有一顆星的圖像。人們可以從0星到許多......所以我需要根據屬性顯示正確的恆星數量。我會嘗試你的方法,謝謝! – emzero 2010-11-23 21:17:04

2

使用concat幫手:

def show_profile_stars(profile) 
    content_tag :span, :class => 'stars' do 
    profile.stars.times do 
     concat(image_tag("stars.gif", :size => "30x30", :class => "gold")) 
    end 

    nil 
    end 
end 

您還需要在content_tag結束返回nil,使其不輸出stars

相關問題