链接器中的link_to与块 [英] link_to in helper with block
问题描述
我正在尝试使它起作用:
I'm trying to get this to work:
link_to("#", class: "add_fields btn btn-success") do
name
content_tag(:i, "", :class => "icon-plus icon-white")
end
但是它只显示i
(twitter-bootstrap css)指定的图标,而不显示name
中的文本,我在做什么错了?
but it only shows me the icon specified by i
(twitter-bootstrap css) and not the text in name
, what am I doing wrong?
推荐答案
该块的返回值成为其内容.仅返回最后一行.
The return value of the block becomes its content. Only the last line is being returned.
您必须将两个字符串与+
串联在一起,以产生单个返回值:
You must concatenate the two strings together with +
to produce a single return value:
link_to("#", class: "add_fields btn btn-success") do
name + content_tag(:i, "", class: "icon-plus icon-white")
end
您需要使用html_safe
来防止标记的内容被自动进行HTML编码:
You'll need to use html_safe
to prevent the content of your tag from automatically being HTML encoded:
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
之间留一个空格:
Speaking from personal experience with Twitter Bootstrap, I know you will need a space between name
and content_tag
:
link_to("#", class: "add_fields btn btn-success") do
name + ' ' + content_tag(:i, "", class: "icon-plus icon-white").html_safe
end
或者,如果您位于ERB模板中,则可以使用<%=
输出这两个值:
Alternatively, if you are inside an ERB template, you can output both values with <%=
:
<%= link_to( ... ) do %>
<%= name %>
<%= content_tag( ... ) %>
<% end %>
这篇关于链接器中的link_to与块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!