Rails, Why does this helper not output HTML, but rather HTML in quotes?

风流意气都作罢 提交于 2020-01-02 01:58:10

问题


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

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

<%= topmenu %>

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


回答1:


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

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



回答2:


It makes the code more clean? Isn't it? And you can do more thing in the help function(because it is not only a html)



来源:https://stackoverflow.com/questions/3878949/rails-why-does-this-helper-not-output-html-but-rather-html-in-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!