背景:我想解码包含实体的字符串,即"c#“应该转换为"c%23”。
我发现HTMLEntities项目通常是被推荐的,但也找到了我认为更简单的解决方案:使用CGI.escape(*string*)
或ERB::Util.url_encode(*string*)
。
问题:,为什么在这个任务中使用CGI.escape或ERB::Util.url_encode是个坏主意?如果是这样的话,如何在Rails 3项目中实现HTMLEntities --我似乎无法从文档中找到它!
发布于 2012-12-02 18:05:42
我不确定每种方法的确切优点。但是,如果您想让want实体正常工作,需要将以下内容添加到Gemfile中:
gem 'htmlentities', :git => "https://github.com/threedaymonk/htmlentities.git"
然后跑:
bundle install
然后,在您的控制器中:
class TestController < ApplicationController
def index
coder = HTMLEntities.new
string = "<élan>" # or whatever string you want to manipulate
@test = coder.encode(string) # => "<élan>"
end
end
然后用@test变量做你想做的任何事情--在视图页面上写出来等等。
https://stackoverflow.com/questions/13671792
复制相似问题