Hi,
I’m implementing some nested forms with Rails 2.3 which works pretty
well as long as I only have to edit the objects that are already
associated to the original object.
I’m not sure though how to implement an “Add new object” button which
adds another associated object. Unfortunately I can’t pre-build
another attribute while loading the form, which would be the standard
way presented in the example form of “What’s new in Rails 2.3”. The
reason why this wouldn’t work for me is that the user first has to
choose which kind of object he’s about to add, so the application
knows which select options to generate the user is able to choose from
to configure the newly created object afterwards.
One way to go presented by Ryan B.’ in his Railscasts (but
designed for Rails 2.2) was to dynamically load an object with a
“link_to_function” request and render it to the fields_for block:
Within the partial associated_object.html.erb this would look like
<% fields_for “object[associated_object][]”, associated_object do |
object_form| %>
…
<% end %>
…with an iterator named “associated_object”.
Within the view there’s:
link_to_function "name" do |page|
page.insert_html :bottom, :associated_objects, :partial =>
‘associated_object’, :object => AssociatedObject.new
end
Unfortunately I don’t understand thoroughly how this one’s working so
I can’t transfer it to the Rails 2.3 situation where the view looks
like this:
<% f.fields_for :associated_objects do |associated_object_form| %>
So what I want to do is:
a) provide a “New” button to dynamically insert a new
associated_object into the view (as done in the Rails 2.2 example)
b) load not just any object into the nested form as done above, but a
specific one the user chooses from a select box before clicking “New”
I’d be very thankful for any advice!! Perhaps there are good examples
on the web? I tried to understand Eloy’s example, but it’s quite
difficult.
Best regards