2012-02-23 66 views
2

我無法完全理解這一點。你會如何將這種多線失敗轉化爲HAML?

原文:

<%= 
Shasta::CSV.generate do |csv| 
    csv << ["Customer", "Job Name", "Payment Date", "Amount", "Notes"] 

    for payment in @payments 
    csv << [ 
     payment.customer.name, 
     payment.jobs.map(&:name).join(" "), 
     I18n.l(payment.created_at, :format => :short_date), 
     number_to_currency(payment.amount), 
     payment.notes 
    ] 
    end 
end.html_safe 
%> 

HTML2HAML它轉換爲這一點,但它失敗

= Shasta::CSV.generate do |csv|          | 
    csv << ["Customer", "Job Name", "Payment Date", "Amount", "Notes"] | 
    for payment in @payments           | 
     csv << [               | 
     payment.customer.name,           | 
     payment.jobs.map(&:name).join(" "),       | 
     I18n.l(payment.created_at, :format => :short_date),   | 
     number_to_currency(payment.amount),       | 
     payment.notes             | 
     ]                | 
    end                | 
    end.html_safe              | 
+1

當這樣的事情發生,它基本上給你,你應該重構它變成一個輔助的提示。 – James 2012-02-23 21:28:40

回答

6

這並不完全回答你的問題,但它是一個痛苦編寫多行haml語句,這就是爲什麼你應該把它移入幫助程序。

= some_descriptive_method_name @payments

def some_descriptive_method_name(payments) 
    Shasta::CSV.generate do |csv| 
    csv << ["Customer", "Job Name", "Payment Date", "Amount", "Notes"] 

    payments.each do |payment| 
     csv << [ 
     payment.customer.name, 
     payment.jobs.map(&:name).join(" "), 
     I18n.l(payment.created_at, :format => :short_date), 
     number_to_currency(payment.amount), 
     payment.notes 
     ] 
    end 
    end.html_safe 
end 
+3

「不要那樣做」是一個非常好的答案。 – 2012-02-23 21:44:46

+0

好點!非常感謝James :) – Trip 2012-02-23 21:44:53

+0

是的,任何時候你需要在HAML中使用不止一行,這是一個標誌,你需要一個輔助方法。 – tadman 2012-02-23 21:48:22