我有一个叫做FeaturesHelper
的助手,它有一些特性的数据:
feature_info = [
{features:
[
{
description: "This is a #{view_context.link_to "link!", "https://www.example.com", target: :_blank, class: "pink-link"}",
}
]
}, ...]
然后我有一个使用feature_info
的部分:
<% feature_info.each do |info|%>
<% info[:features].each do |feature| %>
<%= feature[:description].html_safe %>
<% end %>
<% end %>
然后在视图中渲染局部:
<%= render partial: "partial_name" %>
这会导致堆栈跟踪指向no myFeaturesHelper
:
未定义的局部变量或方法' view_context'为#ActionView::Base:0x007f80a6cdcf20
view_context在这里不可见,您需要将其作为参数传递。
module FeaturesHelper
def self.feature_info(view_context)
[
{
features: [
{
description: "This is a #{view_context.link_to 'link!', 'https://www.example.com', target: :_blank, class: 'pink-link'}",
}
]
}, # ...
]
end
end
关于你的观点:
<% feature_info = FeaturesHelper.feature_info(view_context) %>
<% feature_info.each do |info| %>
<% info[:features].each do |feature| %>
<%= feature[:description].html_safe %>
<% end %>
<% end %>