Ruby-on-rails Rails 嵌套的 content_tag
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4205613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails- nested content_tag
提问by christo16
I'm trying to nest content tags into a custom helper, to create something like this:
我正在尝试将内容标签嵌套到自定义助手中,以创建如下内容:
<div class="field">
<label>A Label</label>
<input class="medium new_value" size="20" type="text" name="value_name" />
</div>
Note that the input is not associated with a form, it will be saved via javascript.
请注意,输入与表单无关,它将通过 javascript 保存。
Here is the helper (it will do more then just display the html):
这是帮助程序(它会做更多的事情,然后只显示 html):
module InputHelper
def editable_input(label,name)
content_tag :div, :class => "field" do
content_tag :label,label
text_field_tag name,'', :class => 'medium new_value'
end
end
end
<%= editable_input 'Year Founded', 'companyStartDate' %>
However, the label is not displayed when I call the helper, only the input is displayed. If it comment out the text_field_tag, then the label is displayed.
但是,当我调用助手时不显示标签,只显示输入。如果它注释掉 text_field_tag,则显示标签。
Thanks!
谢谢!
回答by PeterWong
You need a +to quick fix :D
你需要一个+快速修复:D
module InputHelper
def editable_input(label,name)
content_tag :div, :class => "field" do
content_tag(:label,label) + # Note the + in this line
text_field_tag(name,'', :class => 'medium new_value')
end
end
end
<%= editable_input 'Year Founded', 'companyStartDate' %>
Inside the block of content_tag :div, only the last returned string would be displayed.
在 的块内content_tag :div,只会显示最后返回的字符串。
回答by lmika
You can also use the concatmethod:
您还可以使用concat方法:
module InputHelper
def editable_input(label,name)
content_tag :div, :class => "field" do
concat(content_tag(:label,label))
concat(text_field_tag(name,'', :class => 'medium new_value'))
end
end
end
Source: Nesting content_tag in Rails 3
回答by Sean M
I use a variable and concat to help with deeper nesting.
我使用变量和 concat 来帮助进行更深的嵌套。
def billing_address customer
state_line = content_tag :div do
concat(
content_tag(:span, customer.BillAddress_City) + ' ' +
content_tag(:span, customer.BillAddress_State) + ' ' +
content_tag(:span, customer.BillAddress_PostalCode)
)
end
content_tag :div do
concat(
content_tag(:div, customer.BillAddress_Addr1) +
content_tag(:div, customer.BillAddress_Addr2) +
content_tag(:div, customer.BillAddress_Addr3) +
content_tag(:div, customer.BillAddress_Addr4) +
content_tag(:div, state_line) +
content_tag(:div, customer.BillAddress_Country) +
content_tag(:div, customer.BillAddress_Note)
)
end
end
回答by Sean M
building nested content tags with iteration is a little different and gets me every time... here is one method:
使用迭代构建嵌套内容标签有点不同,每次都会让我感到……这是一种方法:
content_tag :div do
friends.pluck(:firstname).map do |first|
concat( content_tag(:div, first, class: 'first') )
end
end

