I have some html content stored in a database field. I would like trusted admins in our system to be able to edit the HTML when needed. Eventually this html will be converted into an email message and sent to users. In addition to the static html content, the email message has a first and last name that is stored in an active record model.
I was hoping to leave the <%= user.firstname user.lastname %> inside the HTML text when the admins are editing the content and then eventally combine the content with embedded erb the email renderer. I have the part where the admins can save the HTML working fine.
However, even ignoring the email and just trying to render to a normal web page, I am not able to get a view to correctly substitute the embedded <%= ... %> with the information from the database. I always see the <%= ... %> instead of the substituted text. I have tried render_to_string, render :inline, etc.
In summary, I am trying to:
- Take some html text with embedded some erb from the database
- Run the text through a template where the embedded erb is replaced with the variables passed into locals
- Take the resulting text block and either email it to users or display on a web page depending on my needs
The problem is that the results always have the <%= ... %> in it instead of the substitutions.
In an example controller I have:
def showit
@storedhtml = mymodel.savedhtml
@emailtext = render_to_string( template: 'e.html.erb',
layout: false, locals: { user:@user })
end
The e.html.erb contains:
<%= raw @storedhtml %>
and the showit.html.erb has:
= raw @emailtext
I have also tried using inline like this:
def showit
# the line below is substituted but the <p> tags are not converted
@stuff = "<%= '<p>testme</p>' + user.firstname %>"
@storedhtml = mymodel.the_html_content
@output = render( inline: @storedhtml, locals: { user:@user }, layout: true )
end