Doe Doe - 1 month ago 8
Ruby Question

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

I have the next string:

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


Now I want to paste instead of
%{condition}
a text field, so it will look like:

The weather is too __text_field__ today.


I've tried:

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


but it pastes after the whole sentence. How can I put/replace
%{}
with my text field?

max max
Answer

You could use Kernel#sprintf

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

or String#gsub

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