开发者

Using sanitize within a Rails controller

I'm trying to call s开发者_运维问答anitize within a controller. Here's what I tried:

class FooController < ApplicationController
  include ActionView::Helpers::SanitizeHelper
  # ...
end

However, I'm getting this error:

undefined method `white_list_sanitizer' for FooController:Class

I searched around and people recommended switching the include line to include ActionView::Helpers, but that results in this error:

undefined method `url_for' for nil:NilClass

What's the correct way to call sanitize? I'm using Rails 2.3.5.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜