Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3946994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:11:57+00:00 2026-05-20T01:11:57+00:00

The case is simple: I have markdown in my database, and want it parsed

  • 0

The case is simple: I have markdown in my database, and want it parsed on output(*).

@post.body is mapped to the posts.body column in the database. Simple, default Activerecord ORM. That column stores the markdown text a user inserts.

Now, I see four ways to offer the markdown rendered version to my views:

First, in app/models/post.rb:

# ...
def body
   markdown = RDiscount.new(body)
   markdown.to_html
end

Allowing me to simply call @post.body and get an already rendered version. I do see lots of potential problems with that, e.g. on edit the textfield being pre-filled with the rendered HMTL instead of the markdown code.

Second option would be a new attribute in the form of a method

In app/models/post.rb:

# ...
def body_mardownified
   markdown = RDiscount.new(body)
   markdown.to_html
end

Seems cleanest to me.

Or, third in a helper in app/helpers/application_helper.rb

def markdownify(string)
   markdown = RDiscount.new(string)
   markdown.to_html
end

Which is used in the view, instead of <%= body %>, <%= mardownify(body) %>.

The fourth way, would be to parse this in the PostsController.

def index
  @posts = Post.find(:all)
  @posts.each do |p|
    p.body = RDiscount.new(string).to_html
    @rendered_posts << p
  end
end

I am not too familiar with Rails 3 proper method and attribute architecture. How should I go with this? Is there a fifth option? Should I be aware of gotchas, pitfalls or performance issues with one or another of these options?

(*) In future, potentially updated with a database caching layer, or even special columns for rendered versions. But that is beyond the point, merely pointing out, so to avoid discussion on filter-on-output versus filter-on-input :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:11:58+00:00Added an answer on May 20, 2026 at 1:11 am

    Yet another way would be extending the String class with a to_markdown method. This has the benefit of working on any string anywhere in your application

    class String
      def to_markdown
        RDiscount.new(self)
      end
    end
    
    @post.body.to_markdown
    

    normal bold italic

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple case. I basically want to store data about links
I have a relatively simple case where I want to repurpose a Start button
My use case is very simple : I have a GUI application, and inside
Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
I have recorded a very simple test case Using the Selenium IDE integrated with
Simple case I have a Python program that I intend to support on both
i have a simple case of class with static variable and a get function
I have a very simple case that I think would benefit from using templates
I have simple JSF2 application running on glashfish 3.1. I want to add a
Simple case: i put a DataTable in Cache DataTable table = SomeClass.GetTable(); Cache[test] =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.