私たちがやろうとしているのは、コードのチャンクを に保存し、erb
そのstring
コードを で実行することですrun time
。これが私たちが行ったテストです:
1. take out a chunk of the code from a working erb file and,
2. rewrite the erb file with eval.
取り出した erb コードのチャンクを次に示します。
<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 do |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() は国際化のための翻訳メソッドです。
書き換え後のerbファイルは次のとおりです。
<table class="table table-striped">
<% code = find_config_const('task_log_view', 'projectx')%>
<%= eval(code)%>
</table>
書き換える前は、 のchunk of code
間に入り<table>
ます。variableはコードのチャンクをcode
返し、コードのチャンクを実行します。しかし、ここにエラーがあります:string
eval
(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:
上記のコードの何が問題になっていますか? 助けてくれてありがとう。