The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. These helper methods extend Action View making them callable within your template files.

只允许 sanitize 方法中指定的标签和属性输出到页面,防止注入

sanitize(html, options = {})

Sanitizes HTML input, stripping all tags and attributes that aren't whitelisted.

It also strips href/src attributes with unsafe protocols like javascript:, while also protecting against attempts to use Unicode, ASCII, and hex character references to work around these protocol filters.

The default sanitizer is Rails::Html::WhiteListSanitizer. See Rails HTML Sanitizers for more information.

Custom sanitization rules can also be provided.

Please note that sanitizing user-provided text does not guarantee that the resulting markup is valid or even well-formed. For example, the output may still contain unescaped characters like <>, or &.

  • :tags - An array of allowed tags.

  • :attributes - An array of allowed attributes.

  • :scrubber - A Rails::Html scrubber or Loofah::Scrubber object that defines custom sanitization rules. A custom scrubber takes precedence over custom tags and attributes.

module AnnouncementsHelper
def safe_content(content)
sanitize(content, tags: %w(b br))
end
end
<p>
<strong><%= t 'content' %></strong>
<%= safe_content @announcement.content %>
</p>


http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

Rails sanitize的更多相关文章

  1. Rails报找不到sanitize和raw方法的解决

    以下一段代码作用是对html字符串做过滤作用: sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' '))) 不过实际会报 ...

  2. Rails中的缓存

    最近学习Rails. 看到如下代码: <% if notice %> <p id="notice"><%= notice %></p> ...

  3. Rails关闭html_safe字符串过滤

    在某些情况下我希望html的文本中包含一些换行,因为html5产生换行的方法是插入 <br />所以我可以这么写: text = "hello world!<br /> ...

  4. rails中path、url路径解析,routes信息,form_for剖析,link_to示例,路由实例说明

    原创,转载请注明http://www.cnblogs.com/juandx/p/3963023.html  rails中path.url路径解析,routes信息,form_for剖析,link_to ...

  5. rails 字符串 转化为 html

    simple_format http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format http://api ...

  6. nginx中error_page没有生效(nginx+passenger+rails)

    应用部署方式为 nginx + passenger + rails 当我想要用nginx来默认处理400以上状态时,发现在rails返回respose之后,nginx不会再次执行error_page( ...

  7. Ruby on Rails 创建https应用

    1. 创建证书请求文件条件:私钥+证书签名请求+opensslyum install -y opensslmkdir /root/ssl/ && cd /root/ssl/openss ...

  8. Rails 5 开发进阶

    Rails 5 开发进阶:https://www.gitbook.com/book/kelby/rails-beginner-s-guide/details   cancan : http://blo ...

  9. rails程序文件名命名规范

    1 一般文件名是用小写单词加下划线分割,但类的名字用骆驼法.例如 sessions_controller.rb中定义SessionsController. 2 helpers内的文件为辅助类,定义了许 ...

随机推荐

  1. [转]实现一个无法被继承的C++类

    From:http://blog.csdn.net/lazy_tiger/article/details/2224899 一个类不能被继承,也就是说它的子类不能构造父类,这样子类就没有办法实例化整个子 ...

  2. 【C#】透屏幕,屏幕扩展

    if (!SCREEN_STATE) { ) { System.Windows.Forms.Screen s2 = System.Windows.Forms.Screen.AllScreens[]; ...

  3. shell及脚本3——正则表达式

    一.正则表达式 1.1. 什么是正则表达式 正则表达式是处理字符串的方法,以行为单位,通过一些特殊符号的辅助,让用户可以轻易进行查找.删除.替换某特定字符串的操作. 1.2. 正则表达式与通配符的区别 ...

  4. JS常用正则表达式

    1.IP地址验证 var reg = /^(([1-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0 ...

  5. PHP学习总结

    <?php /* PHP简介: PHP是什么:PHP是一种创建动态交互性站点的强有力的服务器端脚步语言 PHP代表Hypertext Preprocessor PHP是一种使用广泛的开源的脚本语 ...

  6. Block 代码快

    1.原理 1.1block类型 a.全局bock块   贯彻整个程序 b.栈块  存在于栈内存中,作用域中使用 c.堆块  自行管理其内存 注*:http://blog.parse.com/learn ...

  7. Git,non-fast-forward

    当把coding好的code,push到Git时会出现这个错误:master[rejected  non-fast-forward] 问题(Non-fast-forward)的出现原因在于:git仓库 ...

  8. NTFS交换数据流隐写的应用

    by Chesky ##目录 ####一.NTFS交换数据流(ADS)简介 ####二.ADS应用 写入隐藏文件(文本\图像\可执行文件) ADS在Windows平台下的利用--写入后门 ADS在We ...

  9. LogStash filter介绍(九)

    LogStash plugins-filters-grok介绍 官方文档:https://www.elastic.co/guide/en/logstash/current/plugins-filter ...

  10. 创建Mat对象的几种方法

    1.Mat的构造函数 Mat M(行数,列数,数据类型,通道数) eg:M(2,2, CV_8UC3, Scalar(0,0,255)). 2.利用Mat的Create()函数.Mat M; M.cr ...