Using sanitize within a Rails controller

后端 未结 3 1965
时光说笑
时光说笑 2020-12-25 12:49

I\'m trying to call sanitize within a controller. Here\'s what I tried:

class FooController < ApplicationController
  include ActionView::Hel         


        
相关标签:
3条回答
  • 2020-12-25 13:09

    you can use this ActionController::Base.helpers inside action method:

    class SiteController < ApplicationController
      def index
        render :text => ActionController::Base.helpers.sanitize('<b>bold</b>')
      end
    end
    

    Hope this helps

    0 讨论(0)
  • 2020-12-25 13:14

    Rails 6:

    To strip links (for example) from a text, just call:

    ...
    Rails::Html::LinkSanitizer.new.sanitize("links here will be stripped")
    ...
    

    see https://github.com/rails/rails-html-sanitizer

    0 讨论(0)
  • 2020-12-25 13:14

    I'm not sure what you're trying to do here but I'm almost 100% certain it doesn't belong in the controller.

    If you want to sanitize an attribute before you save it to the DB, do so in the model with a before save callback.

    Otherwise, sanitize in the view template or view helper.

    0 讨论(0)
提交回复
热议问题