クラスMyClass
class MyClass < ActiveRecord::Base
attr_accessible :attr0, :attr1
end
コントローラMyController
class MyController < ApplicationController
helper_method :set_var
def index
@myclasses = MyClass.all
end
def set_var(foo)
@var0 = foo.attr0
@var1 = foo.attr1
end
end
見る
<table>
<thead>
<tr>
<th>Head0</th>
<th>Head1</th>
</tr>
</thead>
<tbody>
<% @myclasses.each do |myclass| %>
<% set_var(myclass) %>
<tr>
<td><%= @var0 %></td>
<td><%= @var1 %></td>
</tr>
<% end %>
</tbody>
</table>
で操作した後に出力var0
したいのですが、最初に呼び出して MyClass のインスタンスを循環させる必要があります。私が上記で行ったことは、私の見解には何もありません。メソッドset_varでhtmlコードを処理せずにこれを行うエレガントな方法はありますか?var1
set_var
set_var(myclass)
編集:
以下に示すように、私の例は非常に単純化されているため、これはオプションではありません
<% @myclasses.each do |myclass| %>
<tr>
<td><%= myclass.attr0 %></td>
<td><%= myclass.attr1 %></td>
</tr>
<% end %>