Variable mapping with erb

I had the following problem: I have rhtml (html, shredded together with ruby ​​inside the <%%> and <% =%> tags), which is stored in the database that I want to display. Information received by request. I need to evaluate the information that I get from the database, as if it were regular content inside the .erb file. What I have now:

<% @mymods.each do |mod| %>
<%=  render_text(mod["html"])%>
<% end %>

Where mod ["html"] is a variable containing the rhtml code and @mymods array of objects from the request. Currently I do not know which function I should use (render_text, of course, does not work).

Help is appreciated.

/ Tzer0

+3
source share
1

ERB , .
<%= %>. application_helper - .

def render_erb_text(text, args={})
   b = binding
   template = ERB.new(text, 0, "%<>")
   template.result(b)
end

<%=  render_erb_text("<%= %w(hi how are you).join(' - ') %>")%>

, , .

ERB ..

, , . , .

+11

Source: https://habr.com/ru/post/1744436/


All Articles