在 Rails 控制器中使用 sanitize
我正在尝试在控制器内调用 sanitize
。这是我尝试过的:
class FooController < ApplicationController
include ActionView::Helpers::SanitizeHelper
# ...
end
但是,我收到此错误:
undefined method `white_list_sanitizer' for FooController:Class
我四处搜索,人们建议切换包含行以包含 ActionView::Helpers
,但这会导致此错误:
undefined method `url_for' for nil:NilClass
什么是正确的方法调用清理
?我正在使用 Rails 2.3.5。
I'm trying to call sanitize
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在操作方法中使用此
ActionController::Base.helpers
:希望这会有所帮助
you can use this
ActionController::Base.helpers
inside action method:Hope this helps
Rails 6:
要从文本中删除链接(例如),只需调用:
请参阅 https://github .com/rails/rails-html-sanitizer
Rails 6:
To strip links (for example) from a text, just call:
see https://github.com/rails/rails-html-sanitizer
我不确定你想在这里做什么,但我几乎 100% 确定它不属于控制器。
如果您想在将属性保存到数据库之前对其进行清理,请在模型中使用保存前回调来执行此操作。
否则,请在视图模板或视图助手中进行清理。
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.