【问题标题】:Rails 4 - HTML in Helper Returning a HashRails 4 - Helper 中的 HTML 返回哈希
【发布时间】:2016-03-07 17:26:41
【问题描述】:

尝试创建一个简单的帮助器,将其返回到我的视图中:

<hr id="effects">

我创建的助手如下所示:

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

当我打电话时

<%= bar("effects") %>

帮助程序正确显示&lt;hr&gt;,但 id 直接显示到页面,如{:id=&gt;"effects"}

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 methods helper


    【解决方案1】:

    根据documentation

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        相关资源
        最近更新 更多