我有一个文本方面的输入供人们编写一个非常基本的描述,并且此描述可以在段落中。当前,当用户击中返回(Enter(密钥时,除非用户手动写
html标签,否则它不会创建新段落。但是,并非所有用户都知道HTML标签。当用户击中返回密钥时,该如何将其应用于默认情况下。我正在使用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 %>
谢谢!
我建议在呈现@post.description
时使用RedCarpet之类的Markdown解析器。这就是我过去处理这种事情的方式。
根据文档安装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
然后,您可以在视图<%= markdown(@post.description) %>
中使用它来渲染描述。