2013-04-02 143 views
0

我有一個助手接收一組附件集合,並嘗試計算圖像是否正方形。如何處理將項目傳遞給幫助者時的項目集合?

關於我有一個條件,如suggestion_grid_square?(@attachments)和輔助方法的意見。

(我已經簡化了代碼,使其更清楚這個問題的問題)

def suggestion_grid_square?(*attachments) 
    suggestion_column_squares?(1,attachments) 
    end 

    def attachment_square?(attachment) 
    (attachment.file_height.to_f/attachment.file_width) <= 1 
    end 

    private 

    def suggestion_column_squares?(column,*attachments) 
    attachments.each do |attachment| 
     attachment_square?(attachment) 
    end 
    end 

下面的代碼返回我這個錯誤:undefined method file_height for #<Array:0x007fa827e9af30>

app/helpers/suggestions_helper.rb:8:in `attachment_square?' app/helpers/suggestions_helper.rb:15:in `block in suggestion_column_squares?'

知道爲什麼它沒有收到file_height屬性或我在這裏做錯了什麼?

更新我這是怎麼創建集合,我怎麼稱呼的看法助手:

boutique_products = Product.by_most_recent.sold_or_designed_by(boutique).shuffle.first(4) 
boutique_products.each { |product| (@attachments << product.default_attachment_or_first_attachment) } 

.follow-boutique-grid{class: ("square-suggestion-grid" if suggestion_grid_square?(@attachments)) } 

回答

2

suggestion_grid_square?,附件已經從圖示參數(*附件)的陣列。然後你將它傳遞給suggestion_column_squares?,它再次提出了論點。但是因爲附件已經是一個數組了,所以splat只會創建一個數組的數組。所以,你的附件suggestion_column_squares?內部變量的樣子:

[[attachment1, attachment2, etc...]] 

當你調用each,你真的只是通過外部件的時間循環和傳遞一個數組。這就是爲什麼你在數組上調用file_height時出錯,因爲attachment(在每個數組中)並不是你所期望的。

你應該從suggestion_column_squares?刪除附件參數圖示運算符(*):

def suggestion_column_squares?(column, attachments) 
    attachments.each do |attachment| 
    attachment_square?(attachment) 
    end 
end