Daily Archives: November 17, 2007

Little Things I love about Rails – This Time ActiveRecord

When you are setting a foreign key in an object you can do it using the id directly or through the helper method. In this example comments belong_to users.


# Using an object reference
u = User.get(1234)
c = Comment.new
c.user = u
# Old school
x = Comment.new
x.user_id = 1234

Yeah – that is what I like to see – a framework that respects programmer intelligence and gives us choices!

Making TinyMCE work in Ajax With Prototype and Ruby on Rails

Well – it was not nearly as easy as one would expect to make this work – but lots of folks have muddled their way through and I followed in their footsteps and fell into a few holes.

Smart folks should just use this code / plugin – which is pretty and clean

http://inplacericheditor.box.re/demos

I have not used it – but the demos are gorgeous.

So in this post, I do it the hard way. I won’t go into detail – it is all in the code below. Someday this might make a nice plugin – but it still has a flaw that I compensate for. Here is the basic outline: (1) include TinyMCE in the portal once – also init TinyMCE in portal.rhtml like this:


<%= javascript_include_tag "tiny_mce/tiny_mce.js" %>
tinyMCE.init({
mode : "none",
theme : "simple"
});

(2) In the div that will hold the editor – at the end of the output put in some Javascript to activate TinyMCE on the textarea in question, (2A) While you are there resize the columns so the text area is nice and wide, (2B) – before activating TinyMCE – figure out if TinyMCE has been pithed by too much Ajaxing – if a getInstanceById throws an error (even if not found it should return a null) – throwing an error indicates that MCE can’t even tell if something is *not* there. We declare MCE hosed, don’t even try to make it work – because it actually works half way, (2C) if it turns out getInstanceById works (returning null is OK) – we assume MCE is alive and activate MCE on the text area in question.

The next trick is catching the Save and Cancel (1) First we do a save from TinyMCE back to the div with a catch block so that when we are not even using TinyMCE because is unconscious – we will not confuse Prototype throwing errors, then (2) once the Ajax request is complete (onSuccess) we deactivate the TinyMCE attached to out textarea (again in a try/catch to eat errors if MCE has been knocked silly). Putting this in onSuccess is perfect because by then the server is updated and we are milliseconds away from wiping out the div – so we de-associate the MCE from the text area *just* before it vanishes in a puff of smoke.

So what puts MCE into a sleep-like state? When you are on an exit screen and you overwrite the div containing the editor from an external navigation operation. In Toozday – this is like going into Wiki, Editing a page, and then without pressing Save or Cancel you switch to Profile (oops – MCE left in limbo), then when you come back to Wiki and Edit – MCE is in a state unresponsiveness – waiting forever for its lost textarea to come back to it – which will never happen :(

I detect this in the Javascript and simply don’t use a zoned-out MCE – let users type in a text area I say! I warn the user to press Save or Cancel before continuing – hey if I charge their credit card twice – it is their fault!! Sooner or later users will learn to press Save or Cancel.

This condition is naturally reset when the whole page is refreshed – in Tooz this is on a Refresh, All Sites, or Site to Site Navigation. So MCE will start working again – leaving the user to wonder about the IQ of the system they are dealing with! MCE seems to come and go almost on a whim :)

Someday I will figure this out – but it is now on to more other fun stuff. Golly – everything I do ends up with some sticky wicket. Ah that is the fun of it all!

Below is the sweet code – with a lot of cool helpful urls that saved my arse and led me to the solutions. The URL to source is http://toozday.rubyforge.org/svn/trunk/app/

Continue reading