【问题标题】:Applying p html-tag at return key press in textarea with Rails5在使用 Rails5 的 textarea 中按返回键时应用 p html-tag
【发布时间】:2018-02-20 15:13:16
【问题描述】:

我有一个文本区域输入,供人们写一个非常基本的描述,这个描述可以是段落。目前当用户点击返回(回车)键时,它不会创建新段落,除非用户写

HTML 标签手动。但并非所有用户都知道 html-tags。当用户点击返回键时,我如何将其应用为默认值。

我正在使用 simple_form gem:

  <%= simple_form_for @post, html: {multipart: true} do |f| %>
  <%= f.input :description, label: "Description:", input_html: { cols: 66, rows: 5, maxlength: 1500 }, as: :text  %>

我在显示时使用 sanitize gem

  <%= Sanitize.fragment(@post.description, Sanitize::Config::BASIC).html_safe %>

谢谢!

【问题讨论】:

    标签: html ruby-on-rails textarea ruby-on-rails-5 simple-form


    【解决方案1】:

    我建议在渲染@post.description 时使用像redcarpet 这样的降价解析器。我过去就是这样处理这种事情的。

    根据文档安装 redcarpet 后,创建一个像这样的辅助方法(根据需要进行调整):

    def markdown(text)
      return "" unless text.present?
      options = {
        filter_html:     true,
        hard_wrap:       true,
        link_attributes: { rel: 'nofollow', target: "_blank" },
        space_after_headers: true,
        fenced_code_blocks: true
      }
    
      extensions = {
        autolink:           true,
        superscript:        true,
        disable_indented_code_blocks: true
      }
    
      renderer = Redcarpet::Render::HTML.new(options)
      markdown = Redcarpet::Markdown.new(renderer, extensions)
    
      markdown.render(text).html_safe
    end
    

    然后你可以在你的视图中使用它&lt;%= markdown(@post.description) %&gt; 来呈现你想要的描述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多