Ruby on Rails:如何从文本输入中删除/删除/清理脚本、样式标签?

发布于 2024-12-19 17:47:45 字数 480 浏览 4 评论 0原文

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓦然回首 2024-12-26 17:47:45

我一直在使用消毒剂来达到这个目的。效果一直很好。

向您的应用程序添加一个常量:

YourApp::SANITIZE_FILTER = {
  :remove_contents => ['script']
}

然后您可以添加一个辅助方法:

 def sanitize_content(content)
   content = Sanitize.clean(content, YourApp::SANITIZE_FILTER)
 end

I've been using sanitize for this exact purpose. It's been working great.

Add a constant to your app:

YourApp::SANITIZE_FILTER = {
  :remove_contents => ['script']
}

Then you can add a helper method:

 def sanitize_content(content)
   content = Sanitize.clean(content, YourApp::SANITIZE_FILTER)
 end
孤独陪着我 2024-12-26 17:47:45

据我所知,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 run bundle install. That alone should do it.

从﹋此江山别 2024-12-26 17:47:45

使用 SanitizeHelper

string = '<span id="span_is"><br><br><u><i>Hi</i></u></span>'
strip_tags(string) #This Will give you "Hi"

Use SanitizeHelper

string = '<span id="span_is"><br><br><u><i>Hi</i></u></span>'
strip_tags(string) #This Will give you "Hi"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文