Earlier I showed you how to create two models in one form. In this article I will show you how to create many models (more than two) in a single form. Just like in the other article, project has many tasks.
In the new action we need to set up a project with a few tasks. Let's say 5.
Last edited by ryanb (2006-11-06 06:22:48)
Offline
Sorry, I don't have a blog. All articles I have written are on this forum. Maybe some day I'll make my own site though.
Offline
How would you save the form data if we introduced a new relation, People, where one person works on one task (ie. Task now has a :person_id attribute), and we want to use the Person's name when entering the Task data? I guess what I'm asking is, how do you go from having the Person's name in the POST data to adding this Person to the current task? I'm getting errors when I try sticking the Person's name into params[tasks][index][person_name], and I'm not sure how to associate tasks and people if I stick the Person's name into params[people][name]. Thanks in advance!
Offline
This is easy to do if you want a select box where you have a list of people's names. You can then give the select box the name "person_id" so the id of the selected name is set to that attribute upon submit.
However, I'm assuming you want a text field where you insert a person's name? This is possible through what I call a fake attribute. The task doesn't have a "person_name" attribute, but we can make one by creating a getter/setter method for it in the task model:
Offline
Is there any way to use "error_messages_for :tasks[x]" with this technique?
Last edited by jed.hurt (2007-03-07 00:17:41)
Offline
The error_messages_for method looks for an instance variable, so you have to set that before calling it. For example:
Offline
are there any good tutorials on looping through the error messages? in my model i have multiple tasks for example and the validation returns something like this:
Invalid Tasks
Invalid Tasks
thanks for helping a newb
Offline
I'm not sure of any tutorials out there on looping through error messages. Every model has an errors object. It stores the error messages. You can loop through them like this:
Offline
ryanb,
Would it be possible to post an example of an update action using the form provided in this example? I am having trouble getting the correct params call to update the attributes.
Thank you,
Offline
There is another tutorial for the edit/update action. The reason it needs to be separate is because that uses the "id" of the model to keep track of it, where as this uses the index of the model. I'm planning to look into this problem in the future and write a new tutorial on it, I just haven't gotten around to it.
Offline
I see. That is the problem I was having. I saw the other tutorial but I was hoping to have one single _form.html file for both new and edit. Do you think you get to do the tutorial anytime soon?
Thank you for all the other work,
Last edited by cauta (2007-04-04 17:11:42)
Offline
Hi all,
This is working great and I have also used the other tutorials of how to do edit/update actions too.
Now in the def new we add.
Last edited by stevepsharpe (2007-04-06 17:11:17)
Offline
Actually the 5 fields stick around because of this line in the create action:
Offline
Ah I missed that one sorry.
However I am having a weird behavior. Ignore the whole task thing. My main model is Property and I have Image as the secondary model (ie tasks).
Each image has a caption too. So the fields need to be a file field and a text field for the caption.
So if I choose an image and add a caption, but miss out any of the main model's fields for example city or street_name. It then duplicates the file and caption fields. By default I only add one set of fields not 5 as in the example. But if the main model has errors it shows 2 sets of fields, not 1 with the caption duplicated in both sets. If however I leave the file and caption blank and get errors on the main model it stays at just one set of fields for the image as expected.
Once again I hope this all makes sense?
Cheers,
Steve
Offline
Can you post the code?
Offline
Sure, I seemed to have fixed it. I had some code in there that I think was kind of duplicating the code that you suggested I use. See the commented code.
Offline
Yep, that was the problem. The primary difference between the commented code and my code is it will only build the property_image unless all of the form fields are blank. This is a good technique if you don't want to require they make a property_image the same time the Property is create. However, only do one or the other - not both.
Offline
Hi again
I have another question about the building of form elements, like so.
Offline
No sense in doing "1.times". You can just render it once:
Offline