[{"data":1,"prerenderedAt":56},["Reactive",2],{"blog-post-ruby-on-rails-markdown-with-active-record":3},{"slug":4,"title":5,"description":6,"excerpt":7,"date":8,"category":9,"author":10,"keywords":11,"readingMinutes":12,"html":13,"headings":14,"tools":33,"related":34},"ruby-on-rails-markdown-with-active-record","Integrate Markdown in Rails Apps with ActiveRecord","Learn how to add Markdown support to Ruby on Rails applications using ActiveRecord and Redcarpet for blog functionalities.","In this tutorial we're going to demonstrate how you can integrate Markdown in your Ruby on Rails application, especially when the context involves ActiveRecord models.","2024-07-01","Tutorial","Matthew Rathbone","Ruby on rails markdown with active record",2,"\u003Ch2 id=\"introduction-integrating-markdown-with-ruby-on-rails-and-activerecord\">Introduction: Integrating Markdown with Ruby on Rails and ActiveRecord\u003C/h2>\n\u003Cp>In this tutorial we&#39;re going to demonstrate how you can integrate Markdown in your Ruby on Rails application, especially when the context involves ActiveRecord models.\u003C/p>\n\u003Cp>Markdown is a lightweight markup language with plain-text-formatting syntax, and Ruby on Rails, a powerful web application framework for the Ruby programming language. ActiveRecord, an essential part of Ruby on Rails, is the M in MVC (model-view-controller), which is responsible for data handling.\u003C/p>\n\u003Cp>Consider this scenario: Your Rails application includes a blog functionality and you want to provide your users with an interface to write their blog posts in Markdown. For this, you save this Markdown text in your database using ActiveRecord, and then render it as converted HTML in the view.\u003C/p>\n\u003Ch2 id=\"dependencies\">Dependencies\u003C/h2>\n\u003Cp>To effectively handle Markdown in Rails, we are going to make use of the Redcarpet gem. It&#39;s a Ruby library for Markdown processing that smells like butterflies and popcorn.\u003C/p>\n\u003Cp>Include Redcarpet in your Gemfile:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-ruby\">gem \u003Cspan class=\"hljs-string\">&#x27;redcarpet&#x27;\u003C/span>\n\u003C/code>\u003C/pre>\u003Cp>After this, run the bundle command:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-bash\">$ bundle install\n\u003C/code>\u003C/pre>\u003Ch2 id=\"using-activerecord-for-storing-markdown-text\">Using ActiveRecord for storing Markdown text\u003C/h2>\n\u003Cp>Consider a Blog model with a \u003Ccode>content\u003C/code> field where the Markdown text will be stored.\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-ruby\">\u003Cspan class=\"hljs-keyword\">class\u003C/span> \u003Cspan class=\"hljs-title class_\">Blog\u003C/span> &lt; \u003Cspan class=\"hljs-title class_ inherited__\">ApplicationRecord\u003C/span>\n\u003Cspan class=\"hljs-keyword\">end\u003C/span>\n\u003C/code>\u003C/pre>\u003Cp>We store the Markdown formatted text in the \u003Ccode>content\u003C/code> column of our blogs table.\u003C/p>\n\u003Ch2 id=\"rendering-markdown-content-to-html\">Rendering Markdown content to HTML\u003C/h2>\n\u003Cp>To render the blog posts in HTML after being saved as Markdown, we&#39;ll use Redcarpet. Let&#39;s create a helper that can be reused to parse any markdown content.\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-ruby\">\u003Cspan class=\"hljs-keyword\">module\u003C/span> \u003Cspan class=\"hljs-title class_\">ApplicationHelper\u003C/span>\n  \u003Cspan class=\"hljs-keyword\">def\u003C/span> \u003Cspan class=\"hljs-title function_\">render_markdown\u003C/span>(\u003Cspan class=\"hljs-params\">text\u003C/span>)\n    renderer = \u003Cspan class=\"hljs-title class_\">Redcarpet\u003C/span>\u003Cspan class=\"hljs-symbol\">:\u003C/span>\u003Cspan class=\"hljs-symbol\">:Render\u003C/span>\u003Cspan class=\"hljs-symbol\">:\u003C/span>\u003Cspan class=\"hljs-symbol\">:HTML\u003C/span>.new\n    markdown = \u003Cspan class=\"hljs-title class_\">Redcarpet::Markdown\u003C/span>.new(renderer)\n    markdown.render(text).html_safe\n  \u003Cspan class=\"hljs-keyword\">end\u003C/span>\n\u003Cspan class=\"hljs-keyword\">end\u003C/span>\n\u003C/code>\u003C/pre>\u003Cp>We&#39;ve kept the output as \u003Ccode>.html_safe\u003C/code> to return safe HTML back to the view without escaping it.\u003C/p>\n\u003Cp>In our views, we can now make use of this helper method to render markdown:\u003C/p>\n\u003Cpre>\u003Ccode class=\"hljs language-ruby\">&lt;%= render_markdown(\u003Cspan class=\"hljs-variable\">@blog\u003C/span>.content) %&gt;\n\u003C/code>\u003C/pre>\u003Ch2 id=\"preview-markdown-content\">Preview Markdown content\u003C/h2>\n\u003Cp>Let&#39;s take it a step further and integrate a live preview for our Markdown editor. \u003Ca href=\"/blog/how-to-preview-rendered-markdown-in-vim\">Here\u003C/a> is a detailed guide on how to preview markdown.\u003C/p>\n\u003Ch2 id=\"conclusion\">Conclusion\u003C/h2>\n\u003Cp>And that&#39;s a wrap! Your Ruby on Rails application is now integrated with Markdown using ActiveRecord with HTML rendering ability. Harnessing the power of ActiveRecord with Markdown paves way for a lightweight and easy-to-style option.\u003C/p>\n",[15,18,21,24,27,30],{"id":16,"text":17,"level":12},"introduction-integrating-markdown-with-ruby-on-rails-and-activerecord","Introduction: Integrating Markdown with Ruby on Rails and ActiveRecord",{"id":19,"text":20,"level":12},"dependencies","Dependencies",{"id":22,"text":23,"level":12},"using-activerecord-for-storing-markdown-text","Using ActiveRecord for storing Markdown text",{"id":25,"text":26,"level":12},"rendering-markdown-content-to-html","Rendering Markdown content to HTML",{"id":28,"text":29,"level":12},"preview-markdown-content","Preview Markdown content",{"id":31,"text":32,"level":12},"conclusion","Conclusion",[],[35,42,49],{"slug":36,"title":37,"description":38,"excerpt":39,"date":40,"category":9,"author":10,"keywords":41,"readingMinutes":12},"django-markdown","Integrate Markdown in Django: A Step-by-Step Guide","Learn to integrate Markdown into your Django projects with our straightforward tutorial using the Markdown-deux library.","In this tutorial, we are going to learn how to integrate and use Markdown in Django. Very often, we need to process text in Markdown format to output HTML in our Django applications. This can be…","2024-07-22","Django markdown",{"slug":43,"title":44,"description":45,"excerpt":45,"date":46,"category":9,"author":47,"keywords":48,"readingMinutes":12},"rendering-markdown-in-ruby-on-rails","Rendering Markdown in Ruby on Rails","Rendering Markdown in Ruby on Rails is a way to display formatted text within a web application. Markdown is a lightweight markup language that allows users to write in a simple, easy-to-read…","2022-12-07","Matthew Rathbone (with help from OpenAI)","6/7/2023",{"slug":50,"title":51,"description":52,"excerpt":53,"date":54,"category":9,"author":10,"keywords":55,"readingMinutes":12},"rails-turbo-markdown-rendering","Rails Turbo Frames and Markdown Rendering Guide","Learn how to use Rails Turbo Frames for dynamic markdown rendering. This guide includes setup steps, integration tips & code snippets.","Ruby on Rails integrates with Hotwire's Turbo Frames to deliver highly interactive, JavaScript-driven web applications. This guide will illustrate how Turbo Frames can interact with Markdown…","2024-07-05","Rails turbo markdown rendering",1792184940467]