I would use a Model callback to generate the html upon saving the Note:
class Note < ActiveRecord::Base
before_save :generate_html
# ...other stuff...
def generate_html
# 'html' is a field whose value is a HTML version of the 'content' field
self.html = self.content.gsub(/\[{2}([a-z]*?)\]{2}/) { |match| link_to $1, "/notes/show/#{$1}" }
# generate other html...
end
end
In order to actually view the Note at a URL like /notes/show/something
you need to tell Rails how to accept it (Rails 'show' routes usually expect the ID of the record you are showing). In your Notes model, enter something like this (I am assuming that you have a field called url
that is a URL-friendly title):
def Note.find_by_id_or_url id
Note.find :first, :conditions => ['id = ? or url = ?', id, id]
end
And then in the Notes controller, add something like this for the show
action:
@note = Note.find_by_id_or_url(params[:id])
Personally, I would recommend not writing your own syntax parser and just using something like Markdown.