I need replace each word between '--' and '--', every word can be extracted with something like:
labels = @post.body.scan(/--(.+?)--/).flatten
But, I do not know how to generate the form with which to replace the words (i think to use each word as a label). Something like this (but something that does work):
<% if labels.length > 0 %>
<strong> <%= 'Replace labels' %> </strong>
<%= form_tag do %>
<% num = 0 %>
<% labels.length.times do %>
<p>
<%= label_tag labels[num] %><br>
<%= text_field_tag labels[num] %>
<% num = num + 1 %>
</p>
<%end%>
<p>
<%= submit_tag ("Replace") %>
</p>
<% end %>
<% end %>
@post.body = "--Lorem ipsum-- dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et --dolore-- magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. --Duis aute-- irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
labels [--Lorem ipsum--, --dolore--, --Duis aute--]
In this case the form contains three labels and text fields to enter the text to replace the labels.
Edited.
I think a simpler solution might be just using javascript to link a text field with inputs for each label:
Press the run snippet button and try it out.
The rails form would look like this:
Since we edit do all the label editing in javascript and send an updated
post[body]
we don't have to care about the labels in rails.