2.3 Nested Forms, dynamically add an attribute

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 Bates' 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| %>
  <div class="associated_object">
    <%= associated_object_form.object.name %>
...

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

Thanks very much, yeah I'll try to deal with that subject in time.
In the meanwhile I'd be happy enough to find a Rails-like way to
implement a "New" Button which calls a controller action and passes
the value of a select tag to that action.
That's not the nicest workaround, but it should work.
Or I'll just set up the view component as I did in Rails 2.2. At least
I don't have to add setters to the model and Rails 2.3 manages that
automatically.

Best regards

Or I'll just set up the view component as I did in Rails 2.2. At least
I don't have to add setters to the model and Rails 2.3 manages that
automatically.

When Rails 2.3 got released, the first thing I didn't was try to migrate
my former nested form integration to Rails' builtin system, but it
failed, so I reverted back.

If you are concerned with the 15 loc added to the model for the
new_whatever_attribute= and so on, well actually using metaprogramming,
you easily extract this into a plugin and share the code between various
models. I think I will follow this path instead of using rails' builtin
nested form support.

Fernando Perez wrote:

If you are concerned with the 15 loc added to the model for the
new_whatever_attribute= and so on, well actually using metaprogramming,
you easily extract this into a plugin and share the code between various
models. I think I will follow this path instead of using rails' builtin
nested form support.

Having looked at it overnight I can see that the new stuff in 2.3 treats
habtm just like hm. So that if for example you have many users and many
roles and many-to-many relationship between them and you try to use the
new stuff to add a role to a user from a select list it will add a copy
of the role to the roles table. If you've got a unique index on the non
id identifier, like the name, then you'll be in trouble.

It will also issue updates to things it shouldn't. I've cleaned it out
of my code.

But the jscript stuff in the examples for adding new records is quite
useful, I'm fiddling with it now to make it work with old-style nested
forms for adding m-2-m links.

John Small