Rails way to offer modified attributes -


the case simple: have markdown in database, , want parsed on output(*).

@post.body mapped posts.body column in database. simple, default activerecord orm. column stores markdown text user inserts.

now, see 4 ways offer markdown rendered version views:

first, in app/models/post.rb:

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

allowing me call @post.body , rendered version. see lots of potential problems that, e.g. on edit textfield being pre-filled rendered hmtl instead of markdown code.

second option new attribute in form of method

in app/models/post.rb:

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

seems cleanest me.

or, third in helper in app/helpers/application_helper.rb

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

which used in view, instead of <%= body %>, <%= mardownify(body) %>.

the fourth way, parse in postscontroller.

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

i not familiar rails 3 proper method , attribute architecture. how should go this? there fifth option? should aware of gotchas, pitfalls or performance issues 1 or of these options?

(*) in future, potentially updated database caching layer, or special columns rendered versions. beyond point, merely pointing out, avoid discussion on filter-on-output versus filter-on-input :).

yet way extending string class to_markdown method. has benefit of working on string anywhere in application

class string   def to_markdown     rdiscount.new(self)   end end  @post.body.to_markdown 

normal bold italic


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -