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.
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.
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.
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.
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?
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.
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.
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.
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
@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...
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?
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
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...
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.
@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.
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
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?
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 :).
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
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
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)
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!
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!
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.
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
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.
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?
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.
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.
even though you posted this 2 years ago - you just helped me in a big way. I did the same thing. However - the first thing I looked at was: http://apidock.com/rails/Rails/Generator/Spec/klass
I was getting worried that klass was no longer available in the way it was being used. I thought it was going to be a very long night! :P But - it turned out to be a very easy fix - thanks to you! CHEERS!
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!
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?
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 ?
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..
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.
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?
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 :)
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
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
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.
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.
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
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!
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.
Ahaa! I am having exactly this problem (that future ajax submits cause duplicate records) - what exactly do you mean by "get that id back from the server and add that hidden input"? What command would I use to pull the id? And would I add that input into the form?
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.
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.
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
thank's a million for this post!! I had spent lot of my time trying to find out what went wrong...U R AN ANGEL helmerj!!!
By the way...what has h() to do with the problem...!! Everything was going fine till the end of tutorial but i was stuck on this last part. Could you please elaborate the h(). I'm kind'a new rider in rails!!
:p eace!!
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?
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.
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
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
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.
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?
is there going to be screencast on nested forms that covers validation?
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.
Hi, Ryan.
Correct if I'm wrong, but, shouldn't the code in the ApplicationHelper be on this page?
Thanks.
Is this Rails3 compatible?
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.
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
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!
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
@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...
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.
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?
@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
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
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.
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...
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.
@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.
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
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.
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.
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 :).
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.
I get the following error when I try to use add fields:
undefined method `klass' for nil:NilClass
what seems to be the problem?
Sorry
My bad. Ignore the previous one.
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
The JS won't work with formtastic because of the structure. Unfortunately, I haven't found a working equivalent.
(speaking of prototype)
Never mind, got one. Maybe someone can need id..
http://gist.github.com/297483
me again ;)
if someone got the 'add' part working with formtastic, please let me know...
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!
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!
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!".
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.
I should add that this only happens when updating a record which already has nested child records.
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
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.
I was also wondering if anyone has used the nested_form plugin with formtastic successfully?
Great tutorial as always, many thanks!
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
Great cast, as always! I'm wondering if Ryan or anyone has built the user-end of the survey...take survey/show results, etc...?
I get the following error when I try to use add fields:
undefined method `klass' for nil:NilClass
what seems to be the problem ?
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
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.
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
These 2 links might assist :
https://github.com/johnzan/railscasts-episodes-episode-197
http://thoughtsincomputation.com/posts/dynamic-nested-forms-in-rails-3
Regards! akza
http://www.bleet.co.za
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
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
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
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.
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.
even though you posted this 2 years ago - you just helped me in a big way. I did the same thing. However - the first thing I looked at was: http://apidock.com/rails/Rails/Generator/Spec/klass
I was getting worried that klass was no longer available in the way it was being used. I thought it was going to be a very long night! :P But - it turned out to be a very easy fix - thanks to you! CHEERS!
Yep, solved my problem. Thanks!
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.
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
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 ?
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!!!
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?
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.
@Adam try putting title in single quotes
:reject_if => lambda { |a| a['title'].blank? }
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.
Fantastic tutorial, explaining exactly what I was looking for... kudos Ryan.
and indeed, I cry a little when I see this JS soup....
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?
That's just what I needed for an application. Thank
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.
And nested attributes is also currently broken:
https://rails.lighthouseapp.com/projects/8994/tickets/4242-nested-child-only-updates-if-parent-changes
Just saw your comment today :). I'll give it a try, thanks!
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!
For the record, link_to_function, being generic even if not unobtrusive, has been restored to Rails 3.
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
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
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
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
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
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
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!
+1 for validations, it's very troublesome problem.... not easy
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.
Ahaa! I am having exactly this problem (that future ajax submits cause duplicate records) - what exactly do you mean by "get that id back from the server and add that hidden input"? What command would I use to pull the id? And would I add that input into the form?
another couple of extremely useful railscasts Ryan :) (this and part 1) keep up the good work.
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.
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.
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.
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
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...
thank's a million for this post!! I had spent lot of my time trying to find out what went wrong...U R AN ANGEL helmerj!!!
By the way...what has h() to do with the problem...!! Everything was going fine till the end of tutorial but i was stuck on this last part. Could you please elaborate the h(). I'm kind'a new rider in rails!!
:p eace!!
Thanks for this, it also works perfect in Rails 3.1
Can u post ur code for 3.1 ? i seem to be having some problem with this lines:
Are you sorted?
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?
Thanks a million helmerj that really helped me