I'm sure some of you are familiar with Ryan Bates' weekly tutorials on
railscasts.com. A few months ago there was a three-part series
(episodes 73-75) about creating/editing multiple models in one form.
I'm trying to achieve the same but not use obtrusive things like
link_to_function.
Mostly this has been easy enough, but I'm not sure how to rewrite the
add_task_link function (which Ryan makes into a helper at the end of
episode 74):
# projects_helper.rb
def add_task_link(name)
link_to_function name do |page|
page.insert_html :bottom, :tasks, :partial => 'task', :object =>
Task.new
end
end
I should point out that I have sort of been able to solve this by
amending the new action of the tasks controller and adding this to its
responds_to block:
format.js do
render :update do |page|
page.insert_html :bottom, :tasks, :partial => 'projects/
task', :object => Task.new
end
end
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?
On Jan 24, 1:39 pm, Tim Chater <t.cha...@gmail.com> wrote:
> I'm sure some of you are familiar with Ryan Bates' weekly tutorials on
> railscasts.com. A few months ago there was a three-part series
> (episodes 73-75) about creating/editing multiple models in one form.
> I'm trying to achieve the same but not use obtrusive things like
> link_to_function.
> Mostly this has been easy enough, but I'm not sure how to rewrite the
> add_task_link function (which Ryan makes into a helper at the end of
> episode 74):
> # projects_helper.rb
> def add_task_link(name)
> link_to_function name do |page|
> page.insert_html :bottom, :tasks, :partial => 'task', :object =>
> Task.new
> end
> end
> 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:
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.