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?
Thank you for these awesome tutorials. I learned a ton. This particular example does not work -- cannot add questions or answers in Rails 3.0.0. Please help.
If you use Rails 3, then you should remove "h(" in application_helper.rb :
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
For those who use this example under Rails 3 and get 'Uncaught SyntaxError: Unexpected token &' (at least on chrome) - you have to remove h method in helper:
I am using rails3 with jquery. I got it to work by copying and pasting Ryan's code to avoid typo and changing the variable to match my paperclip model for adding more than one image to the parent. After the cut and paste I can remove, but had trouble adding.
1) per artyomst, I got adding new field to work, but the fields were in the wrong location.
I have a newbie jQuery question to go with this. I have a select in my nested model which does a find. I want the conditions of that find to be based on a previous field in the form. Is there a way to either set an instance variable in the controller with jQuery or use it inline to refer to that select, like
$('#selectList').val();?
Thanks in advance.
Brian
How to use render(association.to_s.singularize , :f => builder)
when the partial is
in a diffrent folder ?
in the view there are 3 folders: 'tests', 'questions', 'answers'.
so in '_form' in folder-'question' when there is a call to- link_to_add_fields("Add Answer", test_form, :answers)
i get the error-
missing partial 'tests/answer_fields'.
Since we're in 2011 and using Rails 3.0.4 I thought I'd contribute back this necessary change:
in application_helper.rb, the link_to_add_fields function no longer needs the "h" escape that Rails 2 needed, which surrounded the second argument to link_to_function.
I have been trying to apply this solution to my application but im encountering some errors, im guessing some rails 3 incompatibilities, does anyone know of a rails 3 example of nested forms on the net?
It seems like there is a problem with nested form and uniquness conditions.
If I have many Frobs per Person object, with a condition on the Frobs being that the start_date of a Frob has to be unique for a person, then if, using a nested for, I delete a Frob, and add a Frob with the same date, then I get a uniqueness validation error.
I worked through both screencasts and have a Rails 3 version working. It can be found here: git://github.com/johnzan/railscasts-episodes-episode-197.git
When i add_fields. I want them to be associated to a class, "datepicker" Because when i click on a textfield that i render i want to display the datepicker. But it doesnt show up. It does when i have updated/saved the fields.
Wowza. I ran across this screencast after fighting with the learning curve on JQuery, Rails3, and who knows what else. An absolute lifesaver. I've liked your screencasts before, Ryan, but this one got me to donate some $$$ to you. Huge thanks.
i am already finishing the tutorial. but in the end i can't add a new answer or question by clicking new question or answer. and there is no error. any one can help me?
How can I render nested partial with this plugin. Only partial property_manager_fields is rending but nested partial login_fields is not being rending with click on add_to_link using jQuery
_form.html.erb
<%= f.fields_for :property_managers do |builder| %>
<%= render "property_manager_fields", :f => builder %>
<% end %>
_property_manager_fields.html.erb
<%= f.fields_for :login do |builder| %>
<%= render "login_fields", :f => builder %>
<% end %>
<%= f.text_field :phone %>
<%= link_to_remove_fields "remove", f %>
I'm having an issue of the answers not saving. They params seem to pass: "questions_attributes"=>{"0"=>{"required"=>"0"
"kind"=>"option_select"
"answers_attributes"=>{"1315562216665"=>{"text"=>"asdf"
"_destroy"=>"false"}
"1315562221010"=>{"text"=>"asdfasdfasdf"
"_destroy"=>"false"}
"1315562222968"=>{"text"=>"asdfff"
"_destroy"=>"false"}
"1315562215593"=>{"text"=>"asdf"
"_destroy"=>"false"}
"1315562219528"=>{"text"=>"asdfasdf"
"_destroy"=>"false"}
"1315562218369"=>{"text"=>"asdf"
"_destroy"=>"false"}}
"id"=>"8"
"_destroy"=>"false"
"content"=>"How long have you been a Christian?"}
I am using nested_forms plugin and everything works great. The only thing is how to add a number to each form generated dynamically. For example
1: first field
2: second field
3: third field
I am using JQuery Mobile for markup, and I notice that link_to_add_fields in application_helper.rb doesn't convert the rendering to jquery mobile style. It makes sense, JQuery Mobile would mark up the things with its framework before rendering it to the browser. However, is there a way to get around this problem and render it on server side?
For some reason :child_index isn't working for me in rails 3.0.10. My code is also modified to allow reordering. I am also using formtastic 1.2.4 with haml, and have tried switching the semantic_fields_for to fields_for and have also tried with or without the new_object as an array as a previous commenter noted. Here is the code:
First of all thanks for the great work about nested objects. When i tried your example in my application IE gave me an error that object does not support method for jQuery hide() method. I did everything to figured out problem but none of them worked.
-copied jquery files to /assets/javascripts did not work !
-copied content of jquery.js file to application.js did not work!
-changed application.html.erb file like this
<%= javascript_include_tag "jquery","application", :cache=>true %> did not work.
for some reason, I just removed ":cache => true" from my application.html.erb file and the problem went away. When I did have it I kept getting a "wrong number of arguments (3 for 2)" error that pointed to line #6:
<%= javascript_include_tag "jquery","application", :cache=>true %>
in application.html.erb.
I still cant figure out whey this was breaking the app!
I have one question. Method "destroy" belongs to any rails model, so if I call it means that I violate the MVC concepts. The right way would be remove the model from the controller. Am I wrong?
I am using rails 3.1 and can not get the Update to actually delete the question when i click on the delete link.
The question is disappeared but the destroy command is not sent to the server and therefore when i refresh the page, the question reappears.
Is there any way to get the value of dynamically added text fields( not just the value of one text field) before saving it in my database? I have searched it in google but found no clue. Can any one help me? Thanks in advance.
Hey Ryan, yet another amazing rail-cast. I have learned loads abt ruby and rails just following the videos here. Currently using nested_form gem works wonderfully, but prototype seems to have a conflict with jQuery, any help regarding that would be helpful. Thank you.
I just made a gem for dynamically adding has_many association fields, similar to ryan's nested form gem: https://github.com/ncri/nested_form_fields
It allows arbitrarily deep nesting. You are welcome to play with it and send me feedback.
Where's the difference to the nested_forms gem? I can use the latter with one-to-many associations. Unsure about many-to-many, though I can't see why this wouldn't work, too.
Too bad I wasted so much time implementing your railscast solution in rails 3.2. I just found out that the 'nested_forms' gem is pretty straight forward.
FIW here are some changes I made to your helper methods to let them allow blocks, as the original link_to_function method does:
/app/helpers/form_helper.rb
deflink_to_remove_fields(*args, &block)
if block_given?
element = args.first || {}
html_options = args.second
link_to_remove_fields(capture(&block), element, html_options)
else
name = args[0]
element = args[1]
html_options = args[2] || {}
element.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", html_options)
endenddeflink_to_add_fields(*args, &block)
if block_given?
element = args.first || {}
association = args.second
html_options = args.third
link_to_add_fields(capture(&block), element, association, html_options)
else
name = args[0]
element = args[1]
association = args[2]
html_options = args[3] || {}
new_object = element.object.class.reflect_on_association(association).klass.new
fields = element.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", html_options)
endend
Can anyone help me out in this. I am using dynamic fields to generate the ranges. It all working fine but now I have to put some validation over it, and have to do it on Client side. Now I have spent three valuable days to find out the answer but yet not successful. Is there any way to generate my own predecided id to each dynamic text field instead of current date and time? So that I can do the validation using that Id. Or do any one have any better idea? Any type of help will be appreciated. Thanks in advance.
I have some Javascript validations for the dynamic fields, how do you go about solving the issue with dynamically inserted fields? This gem generates the current datestamp.
In case of unsuccessful update action, all fields that were marked for removing (_destroy = true) will be shown again.
To hide them you can use something like this:
Then. Add an eventListener to the above link rather than to use link_to_function
ruby
+$(document).on("click", "a.link_to_add_fields", function(e){
+ e.preventDefault();
+ var link = $(this);
+ var association = $(this).data("association");
+ var content = $(this).data("content");
+ add_fields(link, association, content);
+});
So, after getting this form created can anyone point me in the direction of how to make it functional for a user to fill out? I am stuck! I'd like to have a radio button to select the answer from each group of possible answers and then make it submittable.
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
why I couldn't nest an add field in another without having a double javascript escaping, resulting to a messy display in the view.
How to use partials in diferent folder to nested forms? Thank you for brillant cast!
Thank you for these awesome tutorials. I learned a ton. This particular example does not work -- cannot add questions or answers in Rails 3.0.0. Please help.
Thank you Ryan! Thanks helmerj for the Rails 3 fix! :)
I'm using jquery and when I add I notice the new field isn't going to the correct place so instead of:
$(link).parent().before(content.replace(regexp, new_id));
I use:
$(link).before(content.replace(regexp, new_id));
and this works well.
Hello. Why dont work "Add answer" and "Add squestion" in Rails 3 Ruby 1.9.2 ?
If you use Rails 3, then you should remove "h(" in application_helper.rb :
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
to
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
Thanks to Ruslan Voloshin from rubyclub.com.ua
Thanks for this. Helped me out on Rails 3.1
@nick Are you using some kind of 3.times do block?
I assume that's what causing the extra fields.
@artyomst thanks for pointing out Rails 3 correction
Hi, while I'm trying to do almost the same with add/remove javascript link I had following error:
undefined method `klass' for nil:NilClass
But I don't really understand why, can someone help me please ?
Thanks in advance.
For those who use this example under Rails 3 and get 'Uncaught SyntaxError: Unexpected token &' (at least on chrome) - you have to remove h method in helper:
link_to_function(name, h("add_fields... -> link_to_function(name, ("add_fields
RoR3 escapes by default ;)
Thanks a lot. You saved my life! :)
I am using rails3 with jquery. I got it to work by copying and pasting Ryan's code to avoid typo and changing the variable to match my paperclip model for adding more than one image to the parent. After the cut and paste I can remove, but had trouble adding.
1) per artyomst, I got adding new field to work, but the fields were in the wrong location.
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
2) per Michael Pope, I modify the jquery script to,
$(link).before(content.replace(regexp, new_id));
Now it seems to work, I can add and remove new fields that gets saved.
Thanks,
John
I have a newbie jQuery question to go with this. I have a select in my nested model which does a find. I want the conditions of that find to be based on a previous field in the form. Is there a way to either set an instance variable in the controller with jQuery or use it inline to refer to that select, like
$('#selectList').val();?
Thanks in advance.
Brian
How are you guys building the "response" part of the survey? I have the survey builder working, but don't know how to get the responses...
I've created a response model that belongs to questions and answers, but now what?
Thanks for any shove in the right direction...
http://railscasts.com/episodes/199-mobile-devices
@artyomst, thanks for your comment. Now I got it working in Rails 3 :)
Thanks helmerj for the fix for rails3 .To remove the h() from the method in ApplicationHelper file.
How to use render(association.to_s.singularize , :f => builder)
when the partial is
in a diffrent folder ?
in the view there are 3 folders: 'tests', 'questions', 'answers'.
so in '_form' in folder-'question' when there is a call to- link_to_add_fields("Add Answer", test_form, :answers)
i get the error-
missing partial 'tests/answer_fields'.
it need to be:
- 'answers/answer_fields'
what i need to change?
thanks
I had to change a little the link_to_function call in ApplicationHelper
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
notice the change from \" to ' ... otherwise they would be converted to &, and not a javascript string delimitor.
Rails 3 & jQuery here.
Great post anyway !!
Since we're in 2011 and using Rails 3.0.4 I thought I'd contribute back this necessary change:
in application_helper.rb, the link_to_add_fields function no longer needs the "h" escape that Rails 2 needed, which surrounded the second argument to link_to_function.
link_to_function(name ("add_fields(this, \"{association}\", \"#{escape_javascript(fields)}\")"))
Excellent screencast! It turned out to very useful for me.
Found one bug.
link_to_add_fields in helpers/application_helper.rb does not allow locals to be passed to render method. I changed it to following to make it working.
https://gist.github.com/834910
Found it from
http://stackoverflow.com/questions/4248970/rails-fields-for-render-partial-with-multiple-locals-producing-undefined-variable
Cool, very helpful topic. I like the trick:
var new_id = new Date().getTime();
Hi
Thanks Ryan for another amazing cast!
I have been trying to apply this solution to my application but im encountering some errors, im guessing some rails 3 incompatibilities, does anyone know of a rails 3 example of nested forms on the net?
Thanks
It seems like there is a problem with nested form and uniquness conditions.
If I have many Frobs per Person object, with a condition on the Frobs being that the start_date of a Frob has to be unique for a person, then if, using a nested for, I delete a Frob, and add a Frob with the same date, then I get a uniqueness validation error.
Ahh, the same happens if I want to swap the dates of two nested Frobs.
I was having issues getting the add function to work properly in Rails 3 + jQuery too, but this solution from ceneon solved it for me:
--------------
Change the link_to_function to:
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
notice the change from \" to ' ... otherwise they would be converted to &, and not a javascript string delimitor.
Estoy trabajando con Rails3 y tambien tengo el error:
undefined method `klass' for nil:NilClass
He aplicado todos los cambios que ya mencionaron pero sigue el error. Por lo que veo, el problema no est
@Ponyo Blue
reflect_on_association(association)
returns nil, make sure that 'association' variable holds correct name of the association for your model.
Espero que entiendes. Mi espanol no es bastante buen.
I worked through both screencasts and have a Rails 3 version working. It can be found here: git://github.com/johnzan/railscasts-episodes-episode-197.git
Hope this helps someone.
how about 'rails', '3.1.0.rc4'? thanks
Thanks for a great post!
I have a java-script related question, i think.
When i add_fields. I want them to be associated to a class, "datepicker" Because when i click on a textfield that i render i want to display the datepicker. But it doesnt show up. It does when i have updated/saved the fields.
Any ideas?
Thanks
/Filip
Hey! For those who have undefined method `klass' for nil:NilClass error see the following stackoverflow thread http://stackoverflow.com/questions/5746006/has-many-through-nested-form-that-can-build-multiply-insctance
Wowza. I ran across this screencast after fighting with the learning curve on JQuery, Rails3, and who knows what else. An absolute lifesaver. I've liked your screencasts before, Ryan, but this one got me to donate some $$$ to you. Huge thanks.
thanks for the tutorial.
i am already finishing the tutorial. but in the end i can't add a new answer or question by clicking new question or answer. and there is no error. any one can help me?
Hi, this was fantastic but when pasting the code to add a question, I get a missing partial error! Taking out the following gets rid of the error??
<%= link_to_add_fields "Add Item", f, :questions %>
Did u get the error ?
For those getting the js-rendered HTML auto-escaped, I've solved like this:
First, if you're using Rails 3.0.8 there's a bug which prevents html_safe and/or escape_javascript to work correctly. Just update to 3.0.9.
Second, in the line in application_helper:
link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
you should remove the h() method call, which double escape the HTML, causing the problem.
Thanks for your great screencasts.
Thank-you!
Thank you very much for this tutorial Ryan!!
Just one quick question: Is there any way to adapt this solution to 'simple_form'?
Thanks in advance!!
I just get a load of un-rendered HTML. Help!
Should have re-read Fablo's post properly. Removing h() fixed it.
Which post that you mentioned here?
I meet the same problem, removed h() and get fixed
anyone know how to make this work with 'rails', '3.1.0.rc4'?
As per this example How can I add Question with 4 Answers with single click on
How can I render nested partial with this plugin. Only partial property_manager_fields is rending but nested partial login_fields is not being rending with click on add_to_link using jQuery
_form.html.erb
<%= f.fields_for :property_managers do |builder| %>
<%= render "property_manager_fields", :f => builder %>
<% end %>
_property_manager_fields.html.erb
<%= f.fields_for :login do |builder| %>
<%= render "login_fields", :f => builder %>
<% end %>
<%= f.text_field :phone %>
<%= link_to_remove_fields "remove", f %>
_login_fields.html.erb
<%= f.text_field :username " %>
js on Rails 3.1.0.rc6 can't work.why?
where did you put nested_form.js ? it might go under vendor/assets/javascript and be required in application.js as
//= require jquery.min
//= require jquery-ui.min
//= require jquery_ujs
//= require nested_form
//= require_tree .
i put under app/assets/javascripts/application.js(includes "function remove_fields(link)"and"function add_fields(link, association, content)") ,
How can I retain a select value after error validation ? :contact_purpose value is gone after validation failed. I have this code into a form:
.emails
= f.fields_for :emails do |builder|
%p
= builder.label :email, "New Contact Email:"
= builder.text_field :email
= builder.select :contact_purpose, options_for_select([['Select', 0], ['Home', 'Home'], ['Work', 'Work']])
= builder.link_to_remove "Remove"
%p= f.link_to_add "Add an Email", :emails
I'm having an issue of the answers not saving. They params seem to pass:
"questions_attributes"=>{"0"=>{"required"=>"0"
"kind"=>"option_select"
"answers_attributes"=>{"1315562216665"=>{"text"=>"asdf"
"_destroy"=>"false"}
"1315562221010"=>{"text"=>"asdfasdfasdf"
"_destroy"=>"false"}
"1315562222968"=>{"text"=>"asdfff"
"_destroy"=>"false"}
"1315562215593"=>{"text"=>"asdf"
"_destroy"=>"false"}
"1315562219528"=>{"text"=>"asdfasdf"
"_destroy"=>"false"}
"1315562218369"=>{"text"=>"asdf"
"_destroy"=>"false"}}
"id"=>"8"
"_destroy"=>"false"
"content"=>"How long have you been a Christian?"}
But it doesn't end up saving.
Models (answer.rb, question.rb, and trip.rb (in place of survey.rb)) are identical to the show notes.
Any ideas on what I'm missing? Thanks!
Disregard this... the
reject_if
was set to reject if the attribute:content
was empty but my attribute was called:text
. Fixed it!I am using nested_forms plugin and everything works great. The only thing is how to add a number to each form generated dynamically. For example
1: first field
2: second field
3: third field
and so on. Any easy way?
did you find a way to do it ?
Can't you just use an ordered list
<ol>
?Hey guys!
I am using JQuery Mobile for markup, and I notice that link_to_add_fields in application_helper.rb doesn't convert the rendering to jquery mobile style. It makes sense, JQuery Mobile would mark up the things with its framework before rendering it to the browser. However, is there a way to get around this problem and render it on server side?
For some reason :child_index isn't working for me in rails 3.0.10. My code is also modified to allow reordering. I am also using formtastic 1.2.4 with haml, and have tried switching the semantic_fields_for to fields_for and have also tried with or without the new_object as an array as a previous commenter noted. Here is the code:
Figured it out, I was passing f instead of builder...
Hello Ryan,
First of all thanks for the great work about nested objects. When i tried your example in my application IE gave me an error that object does not support method for jQuery hide() method. I did everything to figured out problem but none of them worked.
-copied jquery files to /assets/javascripts did not work !
-copied content of jquery.js file to application.js did not work!
-changed application.html.erb file like this
<%= javascript_include_tag "jquery","application", :cache=>true %> did not work.
My rails version 3.1.0
thanks..
for some reason, I just removed ":cache => true" from my application.html.erb file and the problem went away. When I did have it I kept getting a "wrong number of arguments (3 for 2)" error that pointed to line #6:
<%= javascript_include_tag "jquery","application", :cache=>true %>
in application.html.erb.
I still cant figure out whey this was breaking the app!
Hello all,
I have one question. Method "destroy" belongs to any rails model, so if I call it means that I violate the MVC concepts. The right way would be remove the model from the controller. Am I wrong?
PD: sorry, my English isn't that good.
Thanks.
Hi,
I am using rails 3.1 and can not get the Update to actually delete the question when i click on the delete link.
The question is disappeared but the destroy command is not sent to the server and therefore when i refresh the page, the question reappears.
Anyone has any ideas?
thanks,
Liad
+1 validations
+1 too!
Is it possible to use nested forms with a serialized Array attribute?
http://stackoverflow.com/questions/8537588/nested-form-with-embedded-attributes
Can't get it to work.
Getting missing partials in rails 3.1 and the error points to missing block in
Any clue as to why this is coming ?
is your partial in a different view directory than the one it's called from?
An1 tried it with simple_form , i suppose that is the error that i am facing
Has anyone tried to do this with a has_many :through association?
I have three queries:
1) How do I assign and get the unique Id to each text box generated dynamically?
2) How do I resolve the conflict occur between jquery of datepicker and the code <%= javascript_include_tag "application" %>
3) How can I use datepicker function in each text box as datepicker uses the unique Id of each text box.
But... how can I change this
def new
@survey = Survey.new
3.times do
question = @survey.questions.build
4.times { question.answers.build }
end
end
In order to build as much as I want?
I do not why, but only
$(link).prev("input[type=hidden]").val("1");
worked for me in a case of the jquery remove_fields(link) function and rails 3.1.3.
Is there any way to get the value of dynamically added text fields( not just the value of one text field) before saving it in my database? I have searched it in google but found no clue. Can any one help me? Thanks in advance.
Hey Ryan, yet another amazing rail-cast. I have learned loads abt ruby and rails just following the videos here. Currently using nested_form gem works wonderfully, but prototype seems to have a conflict with jQuery, any help regarding that would be helpful. Thank you.
I am having an error, that gets caught on the browser:
Uncaught TypeError: Object [object Object] has no method 'up'
On the line:
What might possibly be the cause for this?? I already saw both railcast and source code, but cannot find the solution
I am getting pretty frustrated on this! I would really appreciate some help here! thanks
I had the same issue.
I fixed the jquery syntax with this:
$(link).parent().before(content.replace(regexp, new_id));
instead of the old syntax (prototype I guess)
I just made a gem for dynamically adding has_many association fields, similar to ryan's nested form gem: https://github.com/ncri/nested_form_fields
It allows arbitrarily deep nesting. You are welcome to play with it and send me feedback.
Where's the difference to the nested_forms gem? I can use the latter with one-to-many associations. Unsure about many-to-many, though I can't see why this wouldn't work, too.
Too bad I wasted so much time implementing your railscast solution in rails 3.2. I just found out that the 'nested_forms' gem is pretty straight forward.
FIW here are some changes I made to your helper methods to let them allow blocks, as the original link_to_function method does:
Can anyone help me out in this. I am using dynamic fields to generate the ranges. It all working fine but now I have to put some validation over it, and have to do it on Client side. Now I have spent three valuable days to find out the answer but yet not successful. Is there any way to generate my own predecided id to each dynamic text field instead of current date and time? So that I can do the validation using that Id. Or do any one have any better idea? Any type of help will be appreciated. Thanks in advance.
Another useful gem for nested_form: https://github.com/ka8725/get_or_build
+1 validations
Hey guys,
I have some Javascript validations for the dynamic fields, how do you go about solving the issue with dynamically inserted fields? This gem generates the current datestamp.
You rock, dude.
Hi guys,
How to refactor link_to_function using UJS since it will be deprecated in Rails 4.
This needs to be refactored for Rails 4 bigtime.
I could be wrong, but remote links ( http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html) are probably what you'd want to use to implement this now, right?
The #196 revised does all this plus the previous video (196) for rails 3, and they mostly work for Rails 4 except for strong parameters adjustments.
I tried it in my project but didn't work yet. for now I have an error says undefined method Klass (that existis in helper). Any help?
ps. I didn't use nifty_layout.. created model normaly
interesting cast. Thanks!
same problem :-(
Did you find a solution yet?
Thank you, it was very helpful! Going to try Nested forms gem right now.
Para los que estan trabajando con la version 3.2.13 de Rails se debe modificar lo siguiente en el archivo "application.js"
Dice:
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}
Debe decir:
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
gracias vsolari, lo que realmente me ayudó.
In case of unsuccessful update action, all fields that were marked for removing (_destroy = true) will be shown again.
To hide them you can use something like this:
I am getting an
undefined method 'link_to_function' for
in my views/surveys/_question_fields.html.erb at this line.<%= link_to_remove_fields "remove", f %><br />
Here is the application_helper.rb
this was a life saver thanks!
Hey guys.
DEPRECATION WARNING: link_to_function is deprecated and will be removed from Rails 4.1.
Hope the following codes could help anyone. ;)
First. Pass args by data-["someAttributes] in a link
Then. Add an eventListener to the above link rather than to use link_to_function
So, after getting this form created can anyone point me in the direction of how to make it functional for a user to fill out? I am stuck! I'd like to have a radio button to select the answer from each group of possible answers and then make it submittable.