我有下一个字符串:
'The weather is too %{condition} today.'
现在我想粘贴而不是%{condition}
一个文本字段,所以它看起来像:
The weather is too __text_field__ today.
我试过了:
if text.include? "%{"
=text_field_tag(:q, nil, class: "")
但它在整个句子之后粘贴。如何在文本字段中添加/替换%{}
?
答案 0 :(得分:2)
您可以使用Kernel#sprintf
= sprintf('The weather is too %s today.',
text_field_tag(:q, nil, class: "")).html_safe
= 'The weather is too %{condition} today.'.
gsub(/%{(\w*)}/, text_field_tag(:q, nil, class: "")).html_safe
答案 1 :(得分:2)
@max的片段已经足够好了,但回答了所说的确切问题,它将是:
= text % {condition: text_field_tag(:q, nil, class: "")).html_safe}
答案 2 :(得分:2)
这将有效
("The weather is too %{condition} today." % { condition: text_field_tag(:q, nil, class: "") }).html_safe