#197
Jan 18, 2010

Nested Model Form Part 2

Add and remove nested model fields dynamically through JavaScript using either Prototype or jQuery.
Tags: forms views
Download (20.2 MB, 12:40)
alternative download for iPod & Apple TV (15.9 MB, 12:40)

Resources

<!-- _form.html.erb -->
<p><%= link_to_add_fields "Add Question", f, :questions %></p>

<!-- _question_fields.html.erb -->
<%= link_to_remove_fields "remove", f %>
...
<p><%= link_to_add_fields "Add Answer", f, :answers %></p>

<!-- _answer_fields.html.erb -->
<%= link_to_remove_fields "remove", f %>
// application.js
function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();
}

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).up().insert({
    before: content.replace(regexp, new_id)
  });
}

// application_jquery.js
function remove_fields(link) {
  $(link).prev("input[type=hidden]").val("1");
  $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).parent().before(content.replace(regexp, new_id));
}
# helpers/application_helper.rb
module ApplicationHelper
  def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end
  
  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
  end
end

RSS Feed for Episode Comments 101 comments

1. mikhailov Jan 18, 2010 at 00:09

Realy nice!
Thanks Ryan


2. Steve Jan 18, 2010 at 01:38

Another helpful episode!


3. Chris Jan 18, 2010 at 02:29

Another excellent tutorial. Looking forward to trying the nested_form plugin that you're working on.


4. Kikan Jan 18, 2010 at 02:49

Nice !

I searched for two months why I couldn't nest an add field in another without having a double javascript escaping, resulting to a messy display in the view.

The "simple" h() you use is the solution and I could get rid of my bug now !

An I like the use of "class" and the separation of Javascript / Ruby, and your convention of naming fields with _fields. It's very clean : I will cleanup my code doing this now.

Thanks a lot.


5. Aditya Sanghi Jan 18, 2010 at 03:31

Hi Ryan,

Looking forward to the nested_form plugin. We already love the Formtastic forms you introduced a few weeks back. I hope we can continue to use that when we try out the nested_form plugin.

Cheers,
Aditya


6. RamOnRails Jan 18, 2010 at 03:53

Ryan! you are very good rubyist!


7. Marcin Jan 18, 2010 at 04:04

Awesome!


8. Nelson Rojas Jan 18, 2010 at 04:31

Great post! Vitamins for start this week :)


9. Samuel Tonini Jan 18, 2010 at 05:13

Just a great screencast as always. :) thanks Ryan.


10. BurmajaM Jan 18, 2010 at 05:55

Nice and useful cast as always. Tnx Ryan.

For those of you that are searching for unobtrusive jQuery version of this functionality, take a look at last answer at http://stackoverflow.com/questions/1704142/unobtrusive-dynamic-form-fields-in-rails-with-jquery.

Hope this helps


11. David Souza Jan 18, 2010 at 06:20

One thing I would say about this is that it is probably better and more intuitive keeping the "remove" part of things separate from the concept of nested forms, don't use "_destroy" fields.

Associations can always be destroyed with standard destroy paths using the Restful API. If you want spiffy dynamic forms, then use AJAX calls to actually destroy the model when you click the link rather than just hiding it and hoping the user knows to update the form.

Another big plus is that the system would still work without javascript which is not true in this case.

Just my two cents.


12. elioncho Jan 18, 2010 at 07:47

class Message < ActiveRecord::Base
  belongs_to :author
  accepts_nested_attributes_for :author
end

I'm using the above and it's working perfectly. The message gets saved with an author_id attribute which references the author (tha author record gets saved too).

The problem:

Sometimes I don't want to save the author because the author already exists (I just want to reference it). So, I just want the message to get saved with the author_id reference. How can I override or modify the normal saving behaviour when using the accepts_nested_attributes_for method?


13. Rick Jan 18, 2010 at 08:53

is there going to be screencast on nested forms that covers validation?


14. Ivan Jan 18, 2010 at 09:14

What if I need to have a 'observe field' in the child class?

For instance, we have a named list with many books (one list has many books) and when one types the ISBN to add it to the list (the number of existent books is too big for a selection field) the system must display the name and price of the book.


15. Javi Jan 18, 2010 at 12:00

Hi, Ryan.

Correct if I'm wrong, but, shouldn't the code in the ApplicationHelper be on this page?

Thanks.


16. Tony Jan 18, 2010 at 14:19

Is this Rails3 compatible?


17. Reza Jan 18, 2010 at 15:40

nitpick, Ryan. The size of the movie files is swapped. Af tirst I'm getting curious, thinking "the iPod file is bigger than the .MOV ?", and proved otherwise. :) Downloading now. Thank you for good screencast, as always.


18. Peter D Jan 18, 2010 at 15:57

Excellent! Like Kikan I was stuck on the escaping problem.. h() never occurred to me as a solution. Thanks once again for a great screencast! :D


19. Tom Jan 18, 2010 at 22:40

Fantastic screencast!

Anyone notice a bug with polymorphic nested tables?

1. create nested attributes on polymorphic model.
2. Add new item, with new sub item (polymorphic) but cause an error on the sub item (eg leave a validated field blank/wrong).
3. Returns to page with errors, anytime trying to submit this fails because of the missing polymorphic values being set.

At the moment, not sure how to fix it, but only enabling the nested model forms on edit screens of the parent item.

Thanks for the screencast!


20. Artur Jan 19, 2010 at 04:47

really nice screencast! just implementing this stuff in my new app


21. Joost Saanen Jan 19, 2010 at 05:13

Another great cast. Thx Ryan! Gonna use it in my applications soon


22. mcansky Jan 19, 2010 at 05:44

really nice screencast arrives just perfectly for one project

but I'm getting a strange error with an apparently non existent association in what would be the same level as :answers, is there any specific rails version involved ? I'm using 2.3.5


23. DGM Jan 19, 2010 at 10:42

@David Souza - There are a couple of ways to think about that issue - some people may not expect any changes until they hit submit, as that's how web pages normally work, at least up until recently.

I like the idea of instant changes too, but perhaps more use feedback should be included, like flashing a message saying it has been deleted, when doing a ajax request...


24. -jul- Jan 19, 2010 at 23:17

I have done it with Haml, Formtastic and jQuery too:

http://blog.js.hu/2009/06/15/add-form-fragments/

Maybe it's a bit old and doesn't have the latest changes but it's still working.


25. Matthew Jan 19, 2010 at 23:56

Very interesting stuff Ryan,

What are your thoughts on using this with a has_and_belongs_to_many relationship? It appears that it aught to work, yet there is no documentation for it. Seems rails doesn't support using this in that way?


26. Patrick Klingemann Jan 20, 2010 at 12:34

@elioncho

I had the same problem. I finally decided to monkey patch some of the nested attributes functionality to add an option :associate_existing to accepts_nested_attributes_for which takes a proc, the result of which is an instance of the associated model. This works pretty well, but it isn't tested. Good luck. Just plop this file into config/initializers

http://gist.github.com/282223


27. Kent Jan 21, 2010 at 01:10

Excellent as always! Keep up the good work!


28. Denis Jan 21, 2010 at 13:25

hey,

Thanks you so much for all your work Ryan!!

for the rails 2.3.4 use _delete instead of _destroy.

this was fix in 2.3.5 see https://rails.lighthouseapp.com/projects/8994/tickets/2889-rename-_delete-to-_destroy-in-nested-attributes


29. mrk Jan 21, 2010 at 13:58

anyone want to take a stab at create a questionnaire from the survey?


30. mrk Jan 21, 2010 at 14:05

Meaning, create a simple model and use formtastic to create all of the questions (from the created survey)... I am stuck on a clean way to do this.


31. Squiddhartha Jan 21, 2010 at 17:23

This stuff looks extremely handy, and I sure wish it had been available three years ago! However, I have to ask... is there any way of getting the same effects without JavaScript?

Yes, I know, we live in an AJAX world now and everybody should just use JavaScript, but it still seems somehow "purer" to me to not *require* it...


32. Dave Jan 22, 2010 at 11:18

I'm curious about how to use Ryan's Populator plug-in with this... and polymorphic associations. I have something sorta working but it seems hacky and brittle.

@Tom, check out Ryan's noted update on Episode 75 for validation of nested attributes.


33. Ryan Bates Jan 23, 2010 at 16:51

@Reza, @Javi, fixed, thanks!

@Rick, validations is something I still need to investigate further, but if I find the need I will consider doing a third part of this series which covers validations. Thanks for the suggestion.

@Ivan, I don't think observe field will work right out of the box with this due to the complexity nesting brings. However you can probably take a look at the generated JavaScript and modify it to work for you.

@Tony, I haven't tested it with Rails 3 yet, but I would guess much of this is still the same.

@Tom, I have yet to investigate polymorphic associations with this, but I'll consider doing an episode on it, thanks for bringing it up.

@mcansky, it should work in Rails 2.3.5, that is the version I'm using. Try downloading the full source code for this episode and see if that works for you. Then compare it to your code to find the difference.

@Matthew, I haven't tried it with HABTM associations and I'm not certain if accepts_nested_attributes_for works with it. I just recommend trying and seeing for yourself.

@Squiddhartha, the previous episode shows as much as one could do without JavaScript. The only major thing missing is the dynamic adding of records which requires JavaScript in order to insert HTML dynamically.

If you want to support it without JavaScript you'll have to handle adding records in a separate step, likely after the parent model is created.

@Dave, I'm not certain what you are referring to with the Populator gem. It works at a lower level than forms so this doesn't really apply there. It also doesn't use ActiveRecord so it can't make use of accepts_nested_attributes_for.


34. Neil Jan 24, 2010 at 13:11

I am having a bit of trouble. I get an "unknown attribute:" error.
my new action creates one empty set of nested fields and this saves fine. when I edit the record and add a new set and save I get the error. Any Ideas? newby in trouble :( Also just for info my models are tax_code and tax_rate with _ .
Thanks in Advance


35. Erik Jan 25, 2010 at 14:46

Great episodes. I am quite new & have a small problem:
I used your nifty_scaffold which adds attr_accessible to the models which I think is then blocking access to the nested attributes.

I have found other web sites referencing this issue saying the solution is to add :modelname_attributes as a parameter to attr_accessible. I have changed modelname to the name of my model, but can't get this to work. I don't get an error, I just don't get my nested fields. Removing the attr_accessible call completely, it all works fine. Is the order of the code in the model important?

Any help appreciated.


36. Erik Jan 25, 2010 at 14:57

Fixed my problem:
I had to specify my nested model name as plural. The other website example I was reading was only a has_one example.

My nested model is called ChecklistItem

so in my top model I have:
attr_accessible :checklist_items_attributes
has_many :checklist_items
accepts_nested_attributes_for :checklist_items

I have left out my other attr_accessible params and other command options for simplicity.


37. Robert Jan 28, 2010 at 01:13

Nice, I was planning on doing something like this but I didnt really figure out how to do it. This solution looks very nice, I think I'll steal it :)


38. bart Jan 30, 2010 at 22:02

Great screencast as usual.

I spent the day trying to figure out how to use this with single table inheritance. I have the situation where I have a form with many different but quite similar entries. e.g. cars trucks bikes. The information I want to gather is just slightly different for each one.

I've been beating my head against it all day and I think it could d be done by changing the helper code to enstantiate the correct form builder and load the correct partial.

Has anyone done something similar and can give any pointers? It feels like the sort of problem that has a very succinct answer if one only knew what it was :).


39. Steve Feb 02, 2010 at 13:38

What if you want a survey to always have at least 1 question field by default?

So when you create a survey, no questions are added.

But when you edit that survey, at least one question field is displayed. When you update the survey, the new question you entered is saved.

I can't currently get this to work. I think update_attributes won't save the new question, but I'm totally lost on how to get it regardless.


40. Ali Feb 03, 2010 at 09:47

I get the following error when I try to use add fields:
undefined method `klass' for nil:NilClass

what seems to be the problem?


41. Ali Feb 03, 2010 at 09:50

Sorry
My bad. Ignore the previous one.


42. Werner Feb 06, 2010 at 07:25

Works fine. My Problem: If I change from a textfield to a datetime select - which is what I need..on clicking the remove field it is disapearing from the view, but not deleting the date in the database.
Any idea? +thanks
Werner


43. paese Feb 07, 2010 at 06:19

The JS won't work with formtastic because of the structure. Unfortunately, I haven't found a working equivalent.

(speaking of prototype)


44. paese Feb 07, 2010 at 06:28

Never mind, got one. Maybe someone can need id..

http://gist.github.com/297483


45. paese Feb 07, 2010 at 07:16

me again ;)

if someone got the 'add' part working with formtastic, please let me know...


46. DerNalia Feb 08, 2010 at 10:57

I can remove / modify the question / answer stuff..
But when I add them, it will show the text boxes for the everything, but when I click submit, nothing is saved to the database. =/

I think it has something to do with :
   def link_to_add_fields(name, f, association)
  
   #create a new instance of the object, association
     new_object = f.object.class.reflect_on_association(association).klass.new
     print new_object
    
     fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
     #render the form partial
       render(association.to_s.singularize + "_fields", :f => builder)
       end
      
     link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
    
   end

there are no errors. It just doesn't commit.
I noticed that your code also has jquery 1.3.2, So I added it to my project in the same spot (don't know if the program adds it automatically though... i haven't added it.. i tried... and nothing happened)

any help you could provide would be awesome!


47. john mccaffrey Feb 08, 2010 at 17:08

Great screencast!
I was able to immediately apply it to a project I'm working on, and get a pretty complicated story done quickly.

(Unfortunately I didn't see some of the recent forks, and ended up doing the unobtrusive jquery stuff myself)

What is the best way to test the helper methods?
I would like to keep our code coverage up, and assert that the js escaping and new_record ids are being assigned, and I tend to test helper methods explicitly, but in this case I'm not sure how to test the 'link_to_add_fields' method, which calls out to the render method.

Has anyone else added tests for these methods? (hopefully I'm missing something obvious)
thanks!


48. Battur Feb 09, 2010 at 01:38

Thank you very much for your dedication to these wonderful screen casts. My little team has started developing apps(for the group companies we work for) with Ruby on Rails for a month, and the speed and the joy just went up by huge amount.

When we face a challenge, we prefer railscast than book, because it's well explained, and very practical. And it works!

We must say "Thank you, Ryan!".


49. Tim Chater Feb 11, 2010 at 08:56

Another great screencast!

I know that validations aren't dealt with here... as far as I can tell they _just work_, with one issue: if a nested record has been removed and the form submission fails validation, that record will be visible again (although it will still be deleted when the valid form is submitted).

I think this is simply because we need a javascript function to run when the body loads which checks for any hidden delete fields with a value of 1 and hides the associated row. Can anyone conjure up the appropriate javascript? Thanks.


50. Tim Chater Feb 11, 2010 at 09:08

I should add that this only happens when updating a record which already has nested child records.


51. Tim Chater Feb 17, 2010 at 08:59

I've worked out a solution to this now.

You just need to add a (conditional) style to the "fields" div to set "display: none" if f.object._destroy


52. Daniel Feb 18, 2010 at 18:41

FYI: Rails3 no longer has a link_to_function.

One option seems to be:
rails plugin install git://github.com/rails/prototype_legacy_helper.git

button_to_function is another option.


53. Chris Masters Feb 20, 2010 at 04:41

I was also wondering if anyone has used the nested_form plugin with formtastic successfully?

Great tutorial as always, many thanks!


54. grary stimon Feb 27, 2010 at 10:12

Thanks Ryan -- really useful.

How would the javascript function change in the case of several child model objects to be enumerated?

So, to extend on your example, what if Survey had models: photo_of_respondent, questions, survey_worker, etc.?

Thanks, Grar


55. KT Mar 01, 2010 at 10:08

Great cast, as always! I'm wondering if Ryan or anyone has built the user-end of the survey...take survey/show results, etc...?


56. Leo Mar 04, 2010 at 06:27

I get the following error when I try to use add fields:

undefined method `klass' for nil:NilClass

what seems to be the problem ?


57. thb Mar 05, 2010 at 13:51

Like said in comment #11 I had done quite the same before inspired by this stack overflow thread. Thanks Ryan for bringing this up with 2 levels of nesting. Here is a unobstrusive jQuery version of the screencast : http://github.com/thb/surveysays


58. Robert Head Mar 08, 2010 at 11:08

Thanks so much, Ryan. Excellent screencasts.

Gotta express my frustration, though. It's absurd that this is so complex. This is probably Rails' most egregious inadequacy.

Why am I spending all morning learning plumbing that should be trivial. I don't mean to be a schmo, but we could do this in one minute in WebObjects in 1997.


59. RubNub Mar 08, 2010 at 12:37

Tried it on Rails 3, there is no link_to_function in Rails 3, can anyone offer me a Rails 3 version of this solution?

Thanks in Advance


60. phlegx Mar 09, 2010 at 07:06

Hi Ryan!

>> NICE CAST! <<

But I have problems with validations. If I catch a form error, the error messages appears but all added questions (e.g. this code) disappear. How can I solve this problem?

Thx


61. ak Mar 15, 2010 at 07:17

undefined method `klass' for nil:NilClass

I am also seeing this error - im obviously doing something stupid here - and I see some other ppl have had(and resolved) the same issue.

could someone kindly explain to me what im missing here.

Thanks in advance!
ak


62. dl Mar 15, 2010 at 10:41

me, to. i'm having the same issue with undefined method `klass' for nil:NilClass. if anyone could shed some light, i'd appreciate it. thanks!

dl


63. dl Mar 15, 2010 at 11:44

ak, i figured out my problem. maybe this will help you. i was trying to incorporate nested model form into an existing project and passed a wrong association to the link_to_add_fields method. hope that helps.


64. equaliser Mar 16, 2010 at 09:10

I also had the undefined method 'klass' for nil:NilClass error - turned out I had put the link_to_add_fields inside the partial, rather than on the parent form.


65. Gabriel Mar 16, 2010 at 22:28

Great screencast Ryan!

I am left with one lingering question, which is, how do you get unique labels for each answer and each question? I don't see how to automate tests for this without being able to make unique labels!

thanks.


66. Cece Mar 17, 2010 at 19:38

Hi. Thanks for the great tutorial.

I just want to ask, how can I use observe_field for the text_field with dynamically generated id?

Because I want to use live validation, and (I think) to be able to do that I have to get the id of the text_field.
My problem is, how can I get it if it's dynamically generated?

Please help me.
Thanks

Cece


67. Boris Mar 18, 2010 at 03:02

Hi, i love nested form bbut i was wondering: how would do you transform your HABTM checkboxes into a nested form, as you have to loop through all categories and check the association with fields_for ?


68. Adam Mar 18, 2010 at 17:09

All works great.. However, if I include ":reject_if => lambda { |a| a[:title].blank? }" The fields wont create or update. If I remove the reject_if everything works fine.

Any ideas why the reject_if would break the create or update?

Also, :title is correct.. That is the field in my database..

Thanks for the tutorials, Ryan!!!


69. Al Mar 24, 2010 at 14:34

Very useful, Ryan. Thanks!

Now that I've built my surveys, how do the users answer them?? Does anyone have sample controllers/views for implementing the user interface?


70. Alex McTammany Apr 06, 2010 at 10:23

So as of Rails 3.beta2 most of this code fails out. Since Javascript has gone the unobtrusive route, the HTML included in the link is gone. Has anyone found a solution to this? I'm just starting to fool around with it.


71. Rafal Apr 07, 2010 at 20:36

@Adam try putting title in single quotes

:reject_if => lambda { |a| a['title'].blank? }


72. klumsyBird Apr 09, 2010 at 21:49

I think this is broken in rails 3. Something to do with how how the new fields are getting linked to with the helper method.


73. Gam Apr 12, 2010 at 21:33

Fantastic tutorial, explaining exactly what I was looking for... kudos Ryan.

and indeed, I cry a little when I see this JS soup....


74. KH Apr 15, 2010 at 14:55

Great tutorial! One question - If I wanted to always have one question per survey and one answer per question by default that the user can't remove, how would I do that?


75. Antoine falquerho Apr 17, 2010 at 21:22

That's just what I needed for an application. Thank


76. Daniel Apr 21, 2010 at 14:03

Note: link_to_add_fields doesn't work in rails3.

check http://pastie.org/928690 for fix. basically fields_for no longer returns the rendered data.


77. Daniel Apr 21, 2010 at 16:22

And nested attributes is also currently broken:

https://rails.lighthouseapp.com/projects/8994/tickets/4242-nested-child-only-updates-if-parent-changes


78. @Patrick Klingemann Apr 23, 2010 at 02:45

Just saw your comment today :). I'll give it a try, thanks!


79. Eloy Duran Apr 27, 2010 at 01:40

And yet another great screencast!

I love how I can relive the discussions we had back when I was implementing this. For instance, seeing the JS which uses the current time as a random id :)

Thanks again for the great work Ryan!


80. Jsmith45 May 03, 2010 at 13:52

For the record, link_to_function, being generic even if not unobtrusive, has been restored to Rails 3.


81. Pepa May 07, 2010 at 12:20

why can't I just add new answer field using jQuery by duplicating the previous one (+maybe changing some numbers in id) and then submit the whole form? can't I avoid the helper method link_to_add_fields ? thanks


82. Stan May 13, 2010 at 23:45

Nice Railscasts espisodes, i want to see a validations episode.


83. Casper Fabricius May 17, 2010 at 05:04

To get this to work in Rails 3, I had to put [] around new_object in the link_to_add_fields method in application_helper.

This is because fields_for_with_nested_attributes only applies the child_index option when given an array, and not a single object.

http://gist.github.com/403687


84. jayan May 31, 2010 at 01:06

In edit form, when i click "Add Answer" and add new answer to an existing question, the new answer does not get saved.In order to save the newly added answer,I must make some modifications in the questions content. However, If i add a new question and add answers to it, that question and all the corresponding answers get saved.

I could not figure out what went wrong? What prevents adding of new answers in the existing question?I am using Rails 3.0.0.beta3


85. dreuff Jun 01, 2010 at 10:07

Thanks a lot for this great screencast !
I experienced some difficulties to adapt it for has_many through relations, but finaly found a nice way without changing to much things.

Source code here : http://pastie.org/987614


86. Fearless Fool Jun 03, 2010 at 11:20

Wonderfully helpful as always.

A stylistic question: might it be preferable to create
  views/questions/_form.html.erb
  views/answers/_form.html.erb
in lieu of
  views/surveys/_question_fields.html.erb
  views/surveys/_answer_fields.html.erb

? As a developer, those would be the places I'd look if I wanted to change the layout or add fields to the questions and answers.

- ff


87. jamesc Jun 09, 2010 at 02:42

Thanks Ryan for the great code, it is just what I needed.

I have this all set up and working well in my project. My only problem is that in one of my nested forms I have a f.select field - this works perfectly in the new.html.erb but I can't get it to show the selected item per the activerecord when I edit it (although the select list shows all the options, so it is working) thanks


88. Lille Jun 13, 2010 at 19:24

I've got this working and I'm happy with it, except for one thing: how do I ensure that any html class specifications are propogated each time a new entry row is created?

For example, in my _field partial, I have a textfield that should be of a certain class:

<%= f.text_field :rent ,:class=>'auto' %>

Unfortunately, the addition of more of these fields with link_to_add does not seem to propagate the html class info!


89. gamov Jun 14, 2010 at 02:18

+1 for validations, it's very troublesome problem.... not easy


90. foo Jun 17, 2010 at 00:29

Turns out that child_index is *not* the id. It is just an index and doesn't really mean anything except that it has to be unique.

Rails associates the id for a given nested record with a particular index by inserting a hidden field like this into the form:

<input type="hidden" value="514" name="contact[emails_attributes][7][id]" id="contact_emails_attributes_7_id">

Where in this case 7 is just an index (normally counts up from 0-(n in collection -1) and 514 is the id.

It doesn't really matter if you do a normal submit but if you want an ajax form that submits on any change, you need to get the id back from the server and add that hidden input or subsequent ajax submits will result in duplicates being created.

Just thought I'd share because it took me a while to figure this out and most references on the web also incorrectly state that it is the id.


91. LeeR Jun 17, 2010 at 15:13

another couple of extremely useful railscasts Ryan :) (this and part 1) keep up the good work.


92. Manjula Jun 24, 2010 at 10:45

Thanks Ryan for the great post.
I've implemented same in my app. But I've problem when I'm using select box in nested.
  My problem is the selected value is not showing in edit.
my select box is like this
<%= f.select(:update_attribute_id,options_for_select([["feature_coach", "67"], ["feature_postbox", "4"], ["feature_audit", "5"], ["employee_model", "6"]]),:class=>"formtextbox") %>

in edit it always shows the first option only. If I pass the id's which is collected form table in arrays like
<%= f.collection_select(:update_attribute_id,options_for_select([["feature_coach", "67"], ["feature_postbox", "4"], ["feature_audit", "5"], ["employee_model", "6"]],[5,6,4]),:class=>"formtextbox") %>
then it shows first id as selected, if i do each then in a single row all the selected will show in seperated box. How could I solve this.

Your input will be very helpful. Thanks in advance.


93. Dale Jul 05, 2010 at 21:03

Probably not the right place to ask, but I really liked the nested_form plugin that Ryan mentions.

However, the remove task link is not working, and when I add a task it adds it to the top of the page.

It seems I have something basic that is wrong.

This is the code that is not working:

http://gist.github.com/464979

I would appreciate it if anyone could point me in the right direction.


94. Dale Jul 05, 2010 at 21:54

Just following up on my own comment above. I got the nested_form plugin to work. Posted up my working example and some notes on what I ran into here: http://gist.github.com/465023

For anyone interested, you should check out Ryan's nested_form plugin as it basically implements exactly what he covers in this screen cast but is a cleaner implementation.


95. nike dunk sb Jul 06, 2010 at 06:53

It is amazing! Thanks!


96. candys Jul 12, 2010 at 01:12

It is amazing! Thanks!


97. olded Jul 12, 2010 at 01:47

All I'm looking for a site on a very large and beautiful on this site are subject to all of you very
thanks


98. flash games Jul 19, 2010 at 01:46

I am apreciating it very much.I have never read such a lovely article and I am coming back tomorrow to continue reading.


99. moncler men's jackets Jul 22, 2010 at 02:42

We are selling all kinds of moncler jackets,moncler coats,moncler vests,moncler shirts and so on,all of our products are at high quality and low price,and they are free shipping.


100. <a href="http://www.ndscardstore.com">NDS</a> Jul 22, 2010 at 19:51

Two weeks ago I have brought
Nintendo DS Cards in http://www.ndscardstore.com/ it's the same site to http://www.ndscardsale.com. Do you where to buy Nintendo DS Cards and Upgrade Revolution?


101. chanel boots sale Jul 23, 2010 at 01:40

they are very comfortable to wear.


102. Nick Bohlen Jul 26, 2010 at 03:31

Hi Ryan,
great screencast.
I'm trying to use this in combination with your recent screencast 217 multistep forms. I have a Site that has many pages.
On the first step i have the pages builder, which works fine according to this screencast. But when I return to the first step from the i.e. the second step everytime it builds more pages_fields.
Has anyone an idea?
Cheers Nick


103. helmerj Jul 28, 2010 at 02:34

For the latest Rails3.rc ONe has to modify a single line:

in "module ApplicationHelper"

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

which removes the not needed h() function.
Otherwise works like charm. No I have to make this work using paperclip file fields...


104. July Jul 30, 2010 at 11:53

Well, I have been checking out this site for a while and find it really cool, I would like to have more time, but right now don-t have it and unfortunately have to spend it getting my <a href="http://www.xlpharmacy.com/"title=''Viagra Without Prescription''>Viagra Without Prescription</a>


105. Car Pictures Jul 31, 2010 at 06:31

All I'm looking for a site on a very large and beautiful on this site are subject to all of you very
thanks


106. Interior Design Jul 31, 2010 at 06:31

Probably not the right place to ask, but I really liked the nested_form plugin that Ryan mentions.


107. jake Aug 01, 2010 at 20:22

Thanks helmerj for the fix for Rails3.rc. However, it only works for adding new fields. It seems Rails3rc busted the 'remove' functionality in this nested model example. Any idea how to fix?


108. new jordans Aug 01, 2010 at 23:13

So as of Rails 3.beta2 most of this code fails out. Since Javascript has gone the unobtrusive route, the HTML included in the link is gone. Has anyone found a solution to this? I'm just starting to fool around with it.


109. timberlandbootsuk Aug 02, 2010 at 02:01

we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and

streamlining trade channels. Also welcome wholesaling, feedback now!


111. logo design Aug 08, 2010 at 23:48

Looking forward to trying the nested_form plugin that you're working on.


112. Conor Aug 10, 2010 at 05:01

Thanks a million helmerj that really helped me


113. UGG Boots on sale Aug 10, 2010 at 18:53

Gooooooooooooooooooood luck ~~!!


114. free directory list Aug 11, 2010 at 22:40

good one site luv that stuff


115. Coach Outlet Aug 12, 2010 at 03:43

Here we have new style Coach Handbags.All the Coach purses are good quality and lower price.A fashion Coach Outlet is dreamed by the fashion females.Welcome to our store discountbagshop.com.I am sure you will find one for yourself.


116. p90 workout Aug 12, 2010 at 09:13

Thanks for writing this. I really feel as though I know so much more about this than I did before. Your blog really brought some things to light that I never would have thought about before reading it. You should continue this, Im sure most people would agree youve got a gift.


117. Air Jordan Retro Aug 14, 2010 at 01:13

Thanks for writing this. I really feel as though I know so much more about this than I did before. Your blog really brought some things to light that I never would have thought about before reading it. So as of Rails 3.beta2 most of this code fails out. Since Javascript has gone the unobtrusive route, the HTML included in the link is gone.


118. Rip Blu-ray for Mac Aug 18, 2010 at 01:49

Thanks,it's so good.
suport!


119. supra tk society Aug 18, 2010 at 18:47

good job,good article


120. nextags Aug 20, 2010 at 01:51

why I couldn't nest an add field in another without having a double javascript escaping, resulting to a messy display in the view.


121. wholesale new era hats Aug 20, 2010 at 20:14

Fantastic Read! Looking forward to more! Bookmarked the site and will be back again!


122. converse all star Aug 20, 2010 at 20:47

love converse all star,love yourself.High quality low price.It's fit for you.


123. omega watches Aug 23, 2010 at 02:39

  winni2078 08 23
http://www.juicycouture4u.com/
http://www.newstyleomega.com/
http://www.juicycouture4u.com/Juicy-Couture-Handbag.html
http://www.juicycouture4u.com/Juicy-Couture-Tracksuits.html
http://www.newstyleomega.com/
http://www.newstyleomega.com/
http://www.newstyleomega.com/


124. Jucky Aug 24, 2010 at 10:44

How to use partials in diferent folder to nested forms? Thank you for brillant cast!


125. cheap clothing Aug 24, 2010 at 19:29

David Heinemeier Hansson..thanks


126. PDF to Images Converter Aug 24, 2010 at 23:00

Some times, to a certain need, we have to convert PDF to image for enjoyment.


127. Wholesale Electronics Aug 25, 2010 at 01:14

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


128. cool stuff Aug 25, 2010 at 02:15

very cool article ,like my cool stuff .very useful.thanks for sharing the article!


129. louis vuitton shoes Aug 26, 2010 at 20:53

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


130. LOGO Design Aug 27, 2010 at 21:04

Very nice screencast as always. Very easy to follow.
Thanks


131. rap Aug 29, 2010 at 09:02

very cool article ,like my cool stuff. very nice. thanks


132. snow boots Aug 30, 2010 at 20:33

I couldn't nest an add field in another without having a double javascript escaping,


133. Cheap Supra Shoes Aug 31, 2010 at 23:57

Hope to view more articles from this writer.


134. GHD Hair Straighteners Aug 31, 2010 at 23:58

Like it very much.


135. GHD Australia Aug 31, 2010 at 23:59

perfect contents


136. blu ray ripper Sep 02, 2010 at 00:36

This is really a nice guide for Newbies like me. Thank you.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source