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

时间:2016-10-13 11:18:24

标签: ruby-on-rails ruby haml

我有下一个字符串:

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

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

The weather is too __text_field__ today.

我试过了:

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

但它在整个句子之后粘贴。如何在文本字段中添加/替换%{}

3 个答案:

答案 0 :(得分:2)

您可以使用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

答案 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