【问题标题】:Sanitizing Trix input清理 Trix 输入
【发布时间】:2018-02-22 09:46:02
【问题描述】:

在 Rails 5.1 下,我使用 Trix 允许用户编辑他们的“法律条件”。然后我试图在更新用户记录之前在我的控制器中清理这个“合法”参数,但最终得到:

undefined method `sanitize'

这里是代码:

params[:user][:legal] = sanitize params[:user][:legal], tags: %w(strong div strong br li ul)

def user_params
  params.require(:user).permit(:presentation, :linktowebsite, :legal)
end

没有看到与此处显示的正常用法不同的任何内容:http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 trix


    【解决方案1】:

    您没有正确使用sanitizesanitize 在视图中使用,而不是在控制器中。

    要正确使用它,您的模型应该允许字段保存用户的 html 输入,但您希望在视图中使用它时“清理”它,以防止不安全或未列入白名单的标签/属性出现发送/显示给用户。

    如果您希望在保存之前删除 html 标签/属性,您可能需要查看strip_tags

    strip_tags("Strip <i>these</i> tags!")
    # => Strip these tags!
    
    strip_tags("<b>Bold</b> no more!  <a href='more.html'>See more here</a>...")
    # => Bold no more!  See more here...
    
    strip_tags("<div id='top-bar'>Welcome to my website!</div>")
    # => Welcome to my website!
    

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 2018-02-18
      • 2012-10-15
      • 2011-07-27
      • 2015-11-10
      • 1970-01-01
      • 2016-11-26
      • 2014-08-12
      • 2013-09-14
      相关资源
      最近更新 更多