2014-02-28 244 views
0

這似乎是一個非常簡單的問題。假設我要檢查動態,以一些屬性添加到一些標籤在一些Helper Module條件:將動態屬性添加到content_tag

def add_tag(hash) 
    content_tag(:div, class: "some_class", rows: "#{check_rows(hash)}") 
end 

def check_rows(hash) 
    hash[:rows].nil? ? "" : hash[:rows] 
end 

這工作得很好,但我不想做rows出現內部生成的標籤,如果hash[:rows].nil?。所以,我想這個

content_tag(:div, class: "some_class", "#{check_rows(hash)}") 

def check_rows(hash) 
    hash[:rows].nil? ? "" : ":rows => hash[:rows]" 
    end 

"#{check_rows(hash)}"不被認可。有沒有辦法做到這一點 ?

+0

你是什麼意思''{#check_rows(hash)}「不被識別」? – mdesantis

回答

0

我認爲你有錯誤的方式content_tag方法的參數。試試這個:

def add_tag(hash) 
    content_tag(:div, check_rows(hash), class: "some_class") 
end 
0

更換

def add_tag(hash) 
    content_tag(:div, class: "some_class", rows: "#{check_rows(hash)}") 
end 

def check_rows(hash) 
    hash[:rows].nil? ? "" : hash[:rows] 
end 

def add_tag(hash) 
    content_tag(:div, "#{check_rows(hash)}", class: "some_class") 
end 

def check_rows(hash) 
    hash[:rows].nil? ? "" : {"rows" => hash[:rows]} 
end 

它不承認是因爲你正經過:rows => hash[:rows]作爲一個字符串,而content_tag預期的哈希的原因。

+0

你也想顯示html內容原始? –

+0

它仍然拋出相同的錯誤 – user1611830

+0

請在這裏聊聊http://chat.stackoverflow.com/rooms/48530/ror –