Rails 4 - HTML 在帮助程序中返回哈希

Rails 4 - HTML in Helper Returning a Hash

尝试创建一个简单的助手,returns我认为:

<hr id="effects">

我创建的助手如下所示:

def bar(id)
  content_tag(:hr, id: id)
end

当我打电话给

<%= bar("effects") %>

助手正确显示 <hr> 但 id 直接显示在页面上,如 {:id=>"effects"}

我假设我做错了,但似乎找不到任何可以帮助我的东西。感谢您的帮助。

content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)

第二个参数是内容,你可以试试

def bar(id)
  content_tag(:hr, "", id: id)
end

根据 documentation

def bar(id)
  content_tag(:hr, "", id: id)
end

但是,我想知道你为什么为此创建了一个辅助方法。您可以直接在视图上执行此操作(也推荐)。