ERBテンプレート内にこのコードを入れて、リスト内のすべての記事をレンダリングします。機能でERBテンプレートコードを再利用するには?
<% @list.each do |a| %>
<article class="my-panel">
<header>
<h2><a href="<%= rel_url_to a[:rel_url] %>"><%= a[:name] %></a></h2>
<time datetime="<%= to_datetime(a[:time]) %>"><%= time_description(a[:time]) %></time>
</header>
... more stuff cut out
</article>
<% end %>
は今、私はこのような何かにそれを変更する必要がありますするつもりです:
<% @list[content_splitter.before_ad_range].each do |a| %>
<%= render_article(a) %>
<% end %>
<%= AdCreator.between_content_ad %>
<% @list[content_splitter.after_ad_range].each do |a| %>
<%= render_article(a) %>
<% end %>
私はhtmlのが私のRubyコードを乱雑にするものではなく、テンプレートにrender_article
を定義しているといいだろうと思いました。しかし、関数内でそのコードを移動すると、エラーが発生します。
これは、関数である:
<% def render_article(a) %>
<article class="my-panel">
<header>
<h2><a href="<%= rel_url_to a[:rel_url] %>"><%= a[:name] %></a></h2>
<time datetime="<%= to_datetime(a[:time]) %>"><%= time_description(a[:time]) %></time>
</header>
<div class="image">
<a href="<%= rel_url_to a[:rel_url] %>"><img alt="" src="<%= rel_url_to a[:img_url_1x] %>" srcset="<%= rel_url_to a[:img_url_2x] %> 2x, <%= rel_url_to a[:img_url_3x] %> 3x"></a>
</div>
<div class="text">
<%= a[:article_text] %>
</div>
</article>
<% end %>
これはエラーです:このエラーを生成するコードの
undefined local variable or method `_erbout' for #<Html::FrontPage:0x0055fb94005c68>
ラインは次のとおりです。
self.class.instance_variable_get(:@renderer).result(binding)
なぜこの出来事はありますか?より有益なエラーを見つけるには?
これを修正するにはどうすればよいですか?この明らかにhtmlの支配的なコードをRubyヘルパーファイルに移動させないでください。
PS。問題は、to_datetime
とtime_description
の機能がERBの内部からアクセスできないということです。
私はその関数render_article
が、私はそれが何をしようとする私は、エラーに
wrong number of arguments (given 1, expected 0)
# (erb):45:in `render_article'