Rails,为什么这个助手不输出 HTML,而是输出引号中的 HTML? [英] Rails, Why does this helper not output HTML, but rather HTML in quotes?

查看:35
本文介绍了Rails,为什么这个助手不输出 HTML,而是输出引号中的 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 application_helper.rb 文件中有以下帮助程序:

I have the following helper in my application_helper.rb file:

  def topmenu
    pages = {
      "projects" => projects_path,
      "photos" => photos_path
    }
    pages.map do |key, value|
      classnames = %( class="current") if controller.controller_name == key
      "<li#{classnames}>#{link_to(key, value)}</li>"
    end
  end

然后在我的 application.html.erb 文件中:

Then in my application.html.erb file I have:

<%= topmenu %>

出于某种原因,生成的页面将来自上述帮助程序的 HTML 显示为 TEXT,而不是 HTML.不知道为什么?谢谢

For some reason, the page is generating showing the HTML from the above helper as TEXT, not HTML. Not sure why? thx

推荐答案

我猜你正在运行 rails3.在返回字符串之前添加 .html_safe 方法调用:

I presume you're running rails3. Add .html_safe method call before returning the string:

"<li#{classnames}>#{link_to(key, value)}</li>".html_safe

这篇关于Rails,为什么这个助手不输出 HTML,而是输出引号中的 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆