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 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| %>

<%= 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

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
Yes, it’s very much on the level of “here’s how to ride a bike, now go
off and race a Formula 1 car, remember to break on the corners”

The example is very fragile, there’s lots of different bits to it all
over the place and they have to fit together just right or it won’t
work. So when you move the example over into your own code you’ll get
caught out trying to work out what has to stay the same and what can be
changed.

I think he expects you to be a hard core coder to be able to understand
what’s going on. But here’s a few clues I’ve picked up from reading
through it.

  1. You’ll need to understand how delegation works in unobtrusive
    javascript. That business of
    blah blah
    is crucial to get the thing to work.
    1. It creates a template for each type of association on the nested
      level, and you’re expected to have a partial with the singularized name
      of the association which can be used to generate the template.

    2. When you create a new nested record, the js code copies the template,
      replaces the string “NEW_RECORD” with a time stamp and puts it inside
      the div with the id set as the pluralized name of the association, at
      the bottom.

    I’m trying to get it to work with has_and_belongs_to_many associations
    and it doesn’t. Reading the code it looks like it can’t work with habtm
    associations without a bit of a rethink, it uses record ids which is
    fine for hm associations, but not for habtm. You mention that you need
    to set things up for different object classes, so it sounds like you
    might be using polymorphic associations. It might not work with them
    either.

    I’ve given up with it, I’m writing my own code to handle habtm.

    John S.

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 P. 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 S.