【问题标题】:Redactor Rails - How to save content to database?Redactor Rails - 如何将内容保存到数据库?
【发布时间】:2013-04-23 18:16:59
【问题描述】:

对于我的应用程序,我正在尝试为“用户”制作的“项目”实现富文本编辑器

现在,我正在尝试实现 Redactor Rails (https://github.com/SammyLin/redactor-rails)。我按照下面的过程。

问题 1:如何使用设计选项重新安装?

问题 2:我在我的数据表中的 Projects 下创建了一个 t.text“描述”。我的项目表单现在使用简单表单。我看到了文本编辑器,但是在按下提交后如何保存在文本编辑器字段中输入的内容?

gem 'redactor-rails'
$ bundle install
$ gem install redactor-rails

添加到 application.js:

//= require redactor-rails

添加到 application.css:

*= require redactor-rails

然后,将以下行放入我的个人“项目”:

<%= text_area_tag :editor, "", :class => "redactor", :rows => 40, :cols => 120 %>

【问题讨论】:

    标签: ruby-on-rails text-editor rich-text-editor redactor


    【解决方案1】:

    你不应该使用 raw text_area_tag 方法。您应该使用 simple_form API 方法。这是一个示例(在 Slim 中,但您应该明白):

    = simple_form_for(comment) do |f|
    
      = f.input :content, input_html: { class: 'redactor', rows: '4' }
    
      = f.button :submit
    

    接下来。 Redactor 不会清理用户的输入。您应该手动完成。

    控制器代码(特别是创建动作)示例:

    class CommentsController
      # 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)
    
      # ...
    
      def create
        params[:comment][:content] = sanitize_redactor(params[:comment][:content])
    
        comment = Comment.create(params[:comment])
    
        if comment.save
          # ...
        end
      end
    
      # ...
    
      private
    
      def sanitize_redactor(orig_text)
        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
    

    【讨论】:

    • 你能帮我理解为什么我们需要清理/清理用户的输入吗?
    • 那是因为您的应用程序等待以通用方式命名的参数。在我的示例中,评论内容应该以 params[:comment][:content] 的形式出现在我的 CommentsController 中(它来自输入字段的 name 属性)。当您使用简单的 text_area_tag 时,您应该自己设置输入的名称。
    • 我们需要清理用户的输入以防止不同类型的黑客攻击,特别是 XSS(当攻击者在您的页面中插入 javascript 代码时)。请在谷歌上搜索 XSS 攻击。
    • 所以我按照你的 f.input 进行了编校,但是当我调用它时,project.content 不是以提交的格式出现的,如何解决?
    • 所以我更新了你提到的 blogupdates_controller.rb 并得到 NameError for: "sanitize_redactor":: def create params[:blogupdate][:content] = sanitize_redactor(params[:blogupdate][:content] ) [at]project = Project.find(params[:project_id]) [at]blogupdate = @project.blogupdates.create!(params[:blogupdate]) redirect_to [at]blogupdate.project 结束
    【解决方案2】:

    对于那些不使用 simple_form 的人,这也有效:

    <div class="redactor_box">
        <%= f.text_area :content, placeholder: "Content goes here...", :class => "redactor"%>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 2011-02-20
      • 1970-01-01
      相关资源
      最近更新 更多