[英]link_to in helper with block
我試圖讓這個工作:
link_to("#", class: "add_fields btn btn-success") do
name
content_tag(:i, "", :class => "icon-plus icon-white")
end
但它只能說明我所指定的圖標i
(Twitter的引導CSS),而不是在文本name
,我究竟做錯了什么?
塊的返回值成為其內容。 只返回最后一行。
你必須連同串聯兩個字符串+
產生一個返回值:
link_to("#", class: "add_fields btn btn-success") do
name + content_tag(:i, "", class: "icon-plus icon-white")
end
您需要使用html_safe
來阻止標記內容自動進行HTML編碼:
link_to("#", class: "add_fields btn btn-success") do
name + content_tag(:i, "", class: "icon-plus icon-white").html_safe
end
根據Twitter Bootstrap的個人經驗,我知道你需要在name
和content_tag
之間content_tag
一個空格:
link_to("#", class: "add_fields btn btn-success") do
name + ' ' + content_tag(:i, "", class: "icon-plus icon-white").html_safe
end
或者,如果您在ERB模板中,則可以使用<%=
輸出兩個值:
<%= link_to( ... ) do %>
<%= name %>
<%= content_tag( ... ) %>
<% end %>
我要考慮兩件事:
1)需要對link_to
塊的全部內容進行清理。
link_to("#", class: "add_fields btn btn-success") do
(name + content_tag(:i, "", class: "icon-plus icon-white")).html_safe
end
2)我們可以期望輸入nil
嗎?
如果我們在一個nil
對象上調用html_safe
,事情就會破裂。 如果有可能發生這種情況,請使用raw
。
link_to("#", class: "add_fields btn btn-success") do
raw(name + content_tag(:i, "", class: "icon-plus icon-white"))
end
對於那些使用font-awesome或其他東西的人來說,它可能不會顯示圖標。 但這個解決方案有效。
link_to :sort => column, :direction => direction do
"#{title} #{content_tag(:i, "", class: "fa fa-chevron-up") }".html_safe
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.