Go to Google Groups Home    Low Pro
Re: Railscasts' complex forms - refactoring with LowPro

Dan Webb <d...@danwebb.net>

> The only problem with this is that I've used a remote link, so it's
> had to go back to the server, rather than all be done client side as
> it was before... is there another way?

The add_task_link helper in that example returns some ugly
HTML/JavaScript.  That's a great example of something that looks
perfectly acceptable at the Ruby end but is in fact pretty damn nasty.

There's a few approaches you could take.  The first one would be to
clone one of the previous task chunks and appendChild to the form.  A
nice way to do this is write a behaviour that clones a copy as soon as
the page is loaded (while the form values are still blank) then makes
clones from that as needed.  The second is that you could basically do
what add_task_link does:

Event.addBehavior({
  '#add_task_link:click': function() {
    $('taskform').insert(<%= render(:partial => 'project/task', ...).to_json);
  }

});

Not that Im just calling render then to_json on the result to turn it
into a JS quoted string.  Third option would be to use DOM Builder.
I'd go with cloning nodes though, it seems cleaner.

--
Dan Webb
http://www.danwebb.net

aim: danwrong123
skype: danwrong