2014-01-24 52 views
0

我正在讀這本書敏捷web開發與rails 4. 有一部分產品的購物車只顯示它不是空的,我的問題是在查看發送給助手只有2個屬性,而在實現中有3個參數。方法屬性[ajax,jquery,rails4]

視圖

我有婁代碼,使細胞對_cart在那裏我有車顯示

<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %> 
     <%= render @cart %> 
<% end %> 

的助手有:

module ApplicationHelper 
def hidden_div_if(condition, attributes = {}, &block) 
if condition 
    attributes["style"] = "display: none" 
end 
content_tag("div", attributes, &block) end 
end 

我的問題是,在這種情況下,&塊收到id:'cart',但它是一個可選attibute?那爲什麼它與&。但是屬性= {}呢? 我真的不知道這是怎麼回事,有人能解釋我一點嗎?

謝謝!

回答

1

doend之間的代碼是塊,並且這是hidden_div_if的第三個參數,該參數僅傳遞給content_taghidden_div_if的定義中的&捕獲了您的視圖中的塊,而調用content_tag時的&再次展開以傳遞它。

answer here用幾個例子很好地解釋了這個想法。我建議您在irb之內自己測試一下,感受它。

+0

偉大的我去吧,我會嘗試做一些測試,讓你知道! – Moh