如何在 Rails 中放置文本字段而不是 %{...}?

How to put text field instead of %{...} in Rails?

我有下一个字符串:

'The weather is too %{condition} today.'

现在我想粘贴而不是 %{condition} 文本字段,因此它看起来像:

The weather is too __text_field__ today.

我试过:

if text.include? "%{"
    =text_field_tag(:q, nil, class: "")

但它粘贴在整个句子之后。我如何 put/replace %{} 使用我的文本字段?

你可以使用 Kernel#sprintf

= sprintf('The weather is too %s today.', 
   text_field_tag(:q, nil, class: "")).html_safe

String#gsub

= 'The weather is too %{condition} today.'.
  gsub(/%{(\w*)}/, text_field_tag(:q, nil, class: "")).html_safe

@max 的片段已经足够好了,但要回答所提出的确切问题,它将是:

= text % {condition: text_field_tag(:q, nil, class: "")).html_safe}

这会起作用

("The weather is too %{condition} today." % { condition: text_field_tag(:q, nil, class: "") }).html_safe