ruby - How to execute view code stored in string in run time with eval in rails 3.2 erb? -


what trying store chunk of erb code in string , execute code in run time. here test did :

1. take out chunk of code working erb file and,  2. rewrite erb file eval.  

here chunk of erb code taken out:

<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.

here erb file after rewriting:

  <table class="table table-striped">     <% code = find_config_const('task_log_view', 'projectx')%>     <%= eval(code)%>    </table> 

before rewriting, chunk of code goes between <table>. variable code returns string of chunk of code , eval execute chunk of code. here error:

(eval):1: syntax error, unexpected '<' (eval):4: syntax error, unexpected tidentifier, expecting $end       <th><%= t('project name') %></th>                ^  extracted source (around line #6):  4:   <table class="table table-striped"> 5:      <% code = find_config_const('task_log_view', 'projectx')%> 6:     <%= eval(code)%> 7:  8:   </table> 9:  

what's wrong code above? help.

eval works evaluating ruby code.

irb(main):001:0> eval('puts "hello world"') hello world => nil 

while converting erb template should done erb class

irb(main):002:0> require 'erb' irb(main):003:0> erb.new('<%= "hello world" %>').result => "hello world" 

my guess code string contain erb template rather ruby code.

nonetheless don't think approach. task.project.name database possibly came user input. doing eval on seems not idea.

probably solve problem normal partials


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -