在 Rails 3 中转义的 HTML [英] HTML escaped in Rails 3

查看:57
本文介绍了在 Rails 3 中转义的 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我有一个这样的方法调用

I have a method call in my view like this

<%= Navigation.with(params) do |menu| 
              if current_user && current_user.can_verify?
                menu.item("Listings", manage_listings_path())
                menu.item("Listing changes", needing_change_approval_manage_listings_path())
                menu.item("Flagged Items", flagged_manage_listings_path())
                menu.item("Transfers", manage_listing_transfers_path())
                menu.item("Reviews", manage_listing_reviews_path())
              end
              if current_user && current_user.admin?
                menu.item("Log", manage_verifications_path())
                menu.item("Indexer Compensations", manage_compensations_path())
                menu.item("Users", manage_users_path())
              end
            end%>

分割出下面的字符串

"<li><a href="/manage/listings" class="active">Listings</a></li> <li><a href="/manage/listings/needing_change_approval">Listing changes</a></li> <li><a href="/manage/listings/flagged">Flagged Items</a></li> <li><a href="/manage/listing_transfers">Transfers</a></li> <li><a href="/manage/listing_reviews">Reviews</a></li> <li><a href="/manage/verifications">Log</a></li> <li><a href="/manage/compensations">Indexer Compensations</a></li> <li><a href="/manage/users">Users</a></li>"

我只是在我的页面中得到了这个字符串.我希望它们是由 CSS 精心设计的菜单.我只是在我的页面中获取上述原始文本.如何将此字符串转换为浏览器将其视为 HTML.

I just get this string in my page. I wanted them to be menus nicely styled by CSS. I am just getting the above raw text in my page. How do I convert this string to be treated as HTML by the browser.

请帮忙

这里是导航类

class NavigationMenu < ActionView::Base
  def initialize(params)
    @params = params
  end

  def item(title, path, options={})
    @items ||= Array.new
    unless (route = Rails.application.routes.recognize_path(path,:method => options[:method]|| :get))
      raise "Unrecognised path #{path}, are you sure it's in routes.rb?"
    end

    @items << content_tag(:li, link_to(title,path, :class => (@params[:controller] == route[:controller] && @params[:action] == route[:action])? 'active' : nil))

  end

  def output
    return '' if @items.blank?
    content_tag(:ul, @items.join("\n"), :id => 'navigation')
  end
end

class Navigation
  def self.with(params, &block)
    menu = NavigationMenu.new(params)
    yield menu
    menu.output
  end
end

推荐答案

您必须添加对 raw 方法的调用:

You have to add a call to the raw method:

<%= raw ... %>

这是必要的,因为在 Rails 3 中,默认情况下每个字符串都会被转义,除非您使用 raw 方法.这就像 Rails 2 中的 h 方法相反,默认情况下每个字符串都是未转义的,除非您使用 h 方法.

This is necessary, because in Rails 3 every string is escaped by default, unless you use the raw method. It's like an inverse of the h method in Rails 2, where every string is unescaped by default, unless you use the h method.

示例:

Rails 2 中的这段代码...

This code in Rails 2...

<%= h "String which must be escaped" %>
<%= "String which must be output raw %>

... 在 Rails 3 中必须是这个:

... must be this in Rails 3:

<%= "String which must be escaped" %>
<%= raw "String which must be output raw %>

(尽管对 h 的额外调用在 Rails 3 中没有任何危害)

(Although an additional call to h doesn't do any harm in Rails 3)

这篇关于在 Rails 3 中转义的 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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