1

我正在尝试使用 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
4

1 回答 1

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
于 2014-10-14T13:58:16.573 回答