【发布时间】:2019-05-15 15:07:56
【问题描述】:
我有一个包含两个块的 ERB 视图:
<%= test_h1 do %>
<%= 'test1' %>
<% end -%>
<%= test_h2 do %>
<%= 'test2' %>
<% end -%>
其中test_h1 和test_h2 是相似的助手,但一个是在助手文件中定义的,而另一个是通过控制器中的helper_method 定义的:
module TestHelper
def test_h1(&block)
link_to '/url' do
capture(&block)
end
end
end
class TestController < ApplicationController
helper_method :test_h2
def test_h2(&block)
helpers.link_to '/url' do
helpers.capture(&block)
end
end
end
test_h1 产生预期的结果,test_h2 首先呈现内部模板块:
<a href="/url">test1</a>
test2<a href="/url"></a>
为什么?写test_h2 的惯用方式是什么?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5 actionview