2016-10-13 16 views
0

我的下一個字符串:如何把文本字段,而不是%{...}在Rails中?

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

現在我要貼的,而不是%{condition}文本字段,所以它看起來像:

The weather is too __text_field__ today. 

我已經試過:

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

但它粘貼在整個句子之後。如何將我的文本字段置換/替換%{}

回答

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 
2

片段由@Max都夠用,但回答問題準確說,這將是:

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

這將工作

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