2011-03-09 44 views
0

我最近將我的應用程序從Rails2轉移到Rails3。給'TemplateError'不能將字符串轉換爲整數

在 '應用程序/視圖/分銷/ index.html.erb' 的代碼是這樣的: -

<div style="padding-bottom:10px; padding-left:0px;float:left;display:<%= (!session[:album][@artist.id.to_s].empty? && !session[:album][@artist.id.to_s].nil?)?'block' : 'none' %>" id = "make_payment_enabled"> 

<%= link_to 'Make Payments',{:action => 'pay', :album=>@album.id}, :class => "button" %> 

</div> 

它給我TemplateError在線: -

<div style="padding-bottom:10px; padding-left:0px;float:left;display:<%= (!session[:album][@artist.id.to_s].empty? && !session[:album][@artist.id.to_s].nil?)?'block' : 'none' %>" id = "make_payment_enabled"> 

如何化解問題?

回答

1

解決方案1:在ERB標籤中,嘗試在'或'問號周圍放置空格,即....nil?) ? 'block...

解決方案更好:做第一步,然後將該代碼放入助手。真的會幫助清理你的觀點。


UPDATE

一些其他提示:你將要切換的條件的順序,因爲你將要看到,如果值是nil檢查,如果它是一個空字符串之前。

調用obj.blank?相當於調用obj.nil? && obj.empty?,這樣可以使代碼更短一些。更好的是,obj.present?!obj.blank?相同。

因此,該行可以簡化爲:

session[:album][@artist.id.to_s].present? ? 'block' : 'none' 

快樂的Rails-ING!