In Rails, how can I allow some html in a text area?

前端 未结 6 2514
灰色年华
灰色年华 2021-02-19 21:35

I have a Rails app (blog) that I am creating. Very basic stuff. In my content area I have a text area for the content of the post. I am needing to include some html in the text

6条回答
  •  心在旅途
    2021-02-19 22:03

    This is an old question but I guess there's a pretty straight forward view helper for this: sanitize. I presume you'd want to render the HTML tags entered by the user. For that purpose, save the content as a string and render as HTML using sanitize.

    Example usage:

    sanitize @post, tags: %w(strong em a code pre h2 h3 p blockquote ul ol li br),
                    attributes: %w(href class)
    

    The tags option allows you to specify which tags to use and same with the html attributes.

提交回复
热议问题