ruby - How to insert string into an erb file for rendering in rails 3.2? -
what trying store chunk of erb
code in string
, render code in run time
in erb.
here chunk of erb code stored string:
<tr> <th>#</th> <th><%= t('date') %></th> <th><%= t('project name') %></th> <th><%= t('task name') %></th> <th><%= t('log') %></th> <th><%= t('entered by') %></th> </tr> <% @logs.each |r| %> <tr> <td><%= r.id %></td> <td><%= (r.created_at + 8.hours).strftime("%y/%m/%d")%></td> <td><%= prt(r, 'task.project.name') %></td> <td><%= prt(r, 'task.task_template.task_definition.name') %></td> <td><%= prt(r, :log) %></td> <td><%= prt(r, 'last_updated_by.name') %></td> </tr> <% end %>
t() translation method internationalization.
how can insert above erb code stored in string erb file below rendering (in pseud code) ?
<table class="table table-striped"> <% erb_code = retrieve(chunk_of_erb_code)%> <% erb_code here rendering %> </table>
is there solution problem? help.
update:
working code render inline
(by kamesh):
<% erb_code = find_config_const('task_log_view', 'projectx')%> <%= render inline: erb.new(erb_code).result(binding) %>
for practice, variable erb_code moved controller.
i think got question. can append html string in erb using in view:
<%= render inline:"<p>new field</p>"%>
or
<%= render inline: "<% products.each |p| %><p><%= p.name %></p><% end %>" %>
or in controller as:
render inline: "<% products.each |p| %><p><%= p.name %></p><% end %>"
writing in _xyz.html.erb or xyz.html.erb , can used controller, well. more check following link. in sub topic - 2.2.6 using render :inline. rails guide
i have checked working of this. let me know in case of issue.
Comments
Post a Comment