Doe Doe - 1 year ago 60
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
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download