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. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  2. SQLSERVER单表CRUD通用方法

    一.适用场景 ①当你书写简单的增删改查心累了 ②当你的项目不考虑并发.高性能 ③当你追求更快速的开发效率 ④当你的业务只涉及单表 二.代码展示 ①单表Insert public bool Insert ...

  3. javascript中针对float的ceil及floor

    function floorFloat(num, precision) { return +(Math.floor(+(num + 'e' + (precision))) + 'e' + -(prec ...

  4. linux基本命令

    常用命令: w 查看登入用户(第一行为主机负载) ifconfig -a 查看所有网络 dhclient 自动获取IP地址 关机命令 init0 shutdown -h now 重启命令 init 6 ...

  5. jQuery 运行机制

    1.Selector,查找元素.这个查找不但包含基于CSS1~CSS3的CSS Selector功能,还包含其对直接查找或间接查找而扩展的一些功能. 2.Dom元素的属性操作.Dom元素可以看作htm ...

  6. POJ1390Blocks(DP+好题+抽空再来理解理解)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4744   Accepted: 1930 Descriptio ...

  7. chose.jquery 联动

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. JVM内存分配策略

    在 JVM内存垃圾回收方法 中,我们已经详细讨论了内存回收,但是,我们程序中生成的对象是如何进行分配的呢?以下所述针对的是HotSpot虚拟机. 1.Java堆结构 以HotSpot为例,如下图: H ...

  9. 仿浏览器TAB效果

    仿浏览器的Tag标签 这里先上个非常非常简陋的demo,没加CSS,我先把jquery的源码给全部搞通,在专心把这个功能给讲一下 <!doctype html> <html lang ...

  10. Echarts 饼图标题文字换行问题

    var option = { title : { text: '数据来源', x:'center' }, tooltip : { trigger: 'item', formatter: "{ ...