2011-12-13 22 views
1

我有這樣的錯誤在我的Rails應用程序:的Rails:IO的實例所需

ActionView::Template::Error (instance of IO needed): 
    30:  <tr> 
    31:   <% @product.images.each do |img| %> 
    32:   <td id="product_image_<%= img.id.to_s %>"> 
    33:    <%= image_tag img.miniature %><br /> 
    34:    <%= link_to_remote raw(t(:delete)), :url => { :action => :de 
lete_image, :id => img.id }, :update => "product_image_#{img.id.to_s}" %> 
    35:   </td> 
    36:   <% end %> 
    app/models/image.rb:45:in `formats_from_yaml' 

img.miniature看起來是這樣的:

Images::formats.each_key do |name| 
    define_method(name) do 
     self.formats_from_yaml[name][:url] 
    end 
    end 

圖像::格式的格式名稱的哈希表作爲一個字符串,像素的寬度和高度以及圖像jpg的質量。

什麼是錯誤?

而且formats_from_yaml這樣的:

def formats_from_yaml 
    YAML.load(self.formats) 
    end 

self.formats與格式的名稱和圖像的URL的哈希值。編輯:self.formats是散列作爲yaml。

令我困惑的是,如果我通過rails控制檯調用Image.first.formats_from_yaml,它將起作用。

什麼是錯誤?

回答

1

YAML.loadtakes an IO stream or string as a parameter並從中加載文檔。如果self.formats是一個哈希,爲什麼傳遞給YAML.load?這似乎是塊應該是:

define_method(name) do 
    self.formats[name][:url] 
end 
+0

格式是YAML,對不起,忘了提它 – JAkk

+0

你可以調用'Image.first.miniature'在控制檯中的哈希?這可能會提示。在預感上,我會在調用'YAML.load'之前將日誌記錄添加到'formats_from_yaml',以確保'self.formats'是您期望它在該上下文中的位置,並嘗試再次加載您的頁面。 ('Rails.logger.info self.formats.inspect') –

+0

Image.first.miniature from console works,it returns the url of the image from the root – JAkk