我有一个包含两个块的 ERB View :
<%= test_h1 do %>
<%= 'test1' %>
<% end -%>
<%= test_h2 do %>
<%= 'test2' %>
<% end -%>
哪里
test_h1
和 test_h2
是类似的助手,但一个是在助手文件中定义的,而另一个是通过 helper_method
定义的。在 Controller 中: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
?
最佳答案
我认为这两个 View 示例都应该重写为:
<%= test_h1 do %>
<% 'test1' %>
<% end -%>
<%= test_h2 do %>
<% 'test2' %>
<% end -%>
我的理解是 '<%=' 强制将块的输出呈现到输出流,这不是这两个示例中的预期行为
关于ruby-on-rails - 捕获 Rails View block 时的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53778131/