Rails - Sanitize Redactor Rails

时间:2013-05-07 00:12:55

标签: ruby-on-rails sanitize

对于我的应用程序,我创建了项目和blogupdates。可以为每个项目创建Blogupdates。我使用redactor rails作为富文本编辑器。它发布很好。

但是回答我的问题HERE,我提到应该对此进行消毒。所以我遵循了建议,在完成清理过程后,我收到以下错误。

问题:有人知道我需要做些什么来解决这个问题,以便消毒有效吗?

NameError in BlogupdatesController#create
undefined local variable or method `orig_text' for #<BlogupdatesController:0x007f9186570700>
app/controllers/blogupdates_controller.rb:68:in `sanitize_redactor'
app/controllers/blogupdates_controller.rb:14:in `create'

blogupdates_controller.rb

class BlogupdatesController < ApplicationController
  # used for sanitization user's input
  REDACTOR_TAGS = %w(code span div label a br p b i del strike u img video audio
              iframe object embed param blockquote mark cite small ul ol li
              hr dl dt dd sup sub big pre code figure figcaption strong em
              table tr td th tbody thead tfoot h1 h2 h3 h4 h5 h6)
  REDACTOR_ATTRIBUTES = %w(href)

  before_filter :authenticate_user! 

  def create
    @project = Project.find(params[:project_id])

    params[:blogupdate][:content] = sanitize_redactor(params[:blogupdate][:content])

    @blogupdate = @project.blogupdates.create!(params[:blogupdate])

    if @blogupdate.save
      redirect_to blogs_project_path(@project), notice: "Blog entry created."
    end   
  end

  private

  def sanitize_redactor(orig_input)
    stripped = view_context.strip_tags(orig_text)
    if stripped.present? # this prevents from creating empty comments
      view_context.sanitize(orig_text, tags: REDACTOR_TAGS, attributes: REDACTOR_ATTRIBUTES)
    else
      nil
    end
  end

end 

1 个答案:

答案 0 :(得分:0)

答案是按如下方式修正以下项目:

def sanitize_redactor(orig_input)
  stripped = view_context.strip_tags(orig_input)
  if stripped.present? # this prevents from creating empty comments
    view_context.sanitize(orig_input, tags: REDACTOR_TAGS, attributes: REDACTOR_ATTRIBUTES)
  else
    nil
  end
end