Ruby on Rails:如何从文本输入中删除/删除/清理脚本、样式标签?
<%= form_for @post.comments.new do |f| %>
Name:<%= f.text_field :name %><br/>
Email:<%= f.text_field :email %><br/>
Body:<%= f.text_area :content %><br/>
<%= submit_tag "Comment" %>
<% end %>
我需要删除/删除/清理 和
标签。
其他选项是如果有任何脚本、样式标签,则输入字段将被视为空。然后评论将不会被保存。
我如何删除这些标签以及这些标签内的代码?我怎样才能删除html标签和html标签内的内容?
<%= form_for @post.comments.new do |f| %>
Name:<%= f.text_field :name %><br/>
Email:<%= f.text_field :email %><br/>
Body:<%= f.text_area :content %><br/>
<%= submit_tag "Comment" %>
<% end %>
I need to delete/remove/sanitize <script>..</script>
and <style>..</style>
tags.
Other option is if there any script,style tags then the input field will be considered as empty. And then the comment will not be saved.
How can i remove those tags and the code inside those tags? And how can i remove html tags too and the contents inside html tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我一直在使用消毒剂来达到这个目的。效果一直很好。
向您的应用程序添加一个常量:
然后您可以添加一个辅助方法:
I've been using sanitize for this exact purpose. It's been working great.
Add a constant to your app:
Then you can add a helper method:
据我所知,Rails 3 中没有任何东西可以做到这一点。使用清理 gem。
将
gem 'sanitize'
添加到您的 Gemfile,然后运行 bundle install
。仅凭这一点就应该做到这一点。As far as I know, nothing in Rails 3 can do this. Use the sanitize gem.
Add
gem 'sanitize'
to your Gemfile, and runbundle install
. That alone should do it.使用 SanitizeHelper
Use SanitizeHelper