我尝试使用content_tag和safe_concat连接多个标签,例如:
content_tag(:select, multiple: "multiple", :name => "contact[resources]") do
content_tag(:optgroup, label: "LABEL", id: "some-id") do
safe_concat(
if condition1
(content_tag(:option, value: "val1") { "Val1" } )
end
if condition2
(content_tag(:option, value: "val2") { "Val2" } )
end
)
end
end
但我一直收到以下错误:
syntax error, unexpected keyword_if, expecting ')'
syntax error, unexpected end-of-input, expecting keyword_end
答案 0 :(得分:1)
你需要在safe_concat
中包装每一个输出,比如
content_tag(:select, multiple: "multiple", :name => "contact[resources]") do
content_tag(:optgroup, label: "LABEL", id: "some-id") do
if condition1
safe_concat(content_tag(:option, value: "val1") { "Val1" } )
end
if condition2
safe_concat(content_tag(:option, value: "val2") { "Val2" } )
end
end
end