Embedding variable inside text using Trix

280 views Asked by At

I would like to let users use variable name inside text box with Trix.

Given a trix text-box that let the user write messages to other user, I would like to be able to do something like this: "Hello %user_name%"

When sending the message the user_name would be changed to whatever is the user_name.

I would except this to be a fairly standard feature but couldn't find mention of it in the documentation or SO.

I have tried doing a simple .gsub on the model.content.body but this is returning and ActionText::RichText who doesn't know what gsub is.

1

There are 1 answers

1
Farhad On

An easy way to mock variables within text contents is to use search and replace, just replace the strings within the action that receives posted contents:

final_message = params[:message].gsub("%user_name%", "John Doe")

Note that gsub is only available for string type.