# 2.3 Nested Forms, dynamically add an attribute

**URL:** <https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981>\
**Category:** Rails\
**Created:** [March 26, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981 "2009-03-26T17:18:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 26, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981/1 "2009-03-26T17:18:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 26, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981/2 "2009-03-26T19:58:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 27, 2009, 11:30am UTC](https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981/3 "2009-03-27T11:30:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 27, 2009, 11:54am UTC](https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981/4 "2009-03-27T11:54:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 27, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/2-3-nested-forms-dynamically-add-an-attribute/163981/5 "2009-03-27T13:10:15Z")

</div>

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.
