In the previous article I showed you how to create a project and a task in the same form. In this article I will show you how to edit a project and all of its tasks in one form.
Let's jump right in. The edit action in the controller is simple enough:
Last edited by ryanb (2006-11-08 16:36:45)
Offline
Thanks again Ryan
Offline
I used this method to successfully edit two models in my app--but how would one go about adding a third model?--AND, what if that third model was called with a collection_select method?
Here's a snippet from my _form.rhtml partial:
Last edited by darrenemo (2006-11-06 09:04:57)
Offline
Here you aren't actuallly editing the Author model. Instead, you are selecting which author goes with the conversation. In other words, you are setting the author_id attribute in the conversation model. You can do it like this:
Offline
Thanks for the reply--ill try it out later
Offline
Hi,
I've found a small issue with the solution below: if any task is not valid, the "all" method stops calling "valid?" for the next tasks.
Orginal:
Offline
Good catch taoweb! However, won't your suggestion still have the same problem? I'm assuming any? will stop on the first true that is returned. I haven't tested it though.
To solve this I suppose it can be split into two lines:
Offline
ryanb wrote:
Does that work for you?
Yes! Thanks dude
Offline
Rails actually has this functionality built in already. Note the [] in the form_for call:
<%
conversation = Conversation.find(21) # could be assigned e.g. in a loop
form_for "conversation[]", conversation do |f| %>
<%= f.text_field :message %>
<% end %>
leads to
<input name="conversation[21][message]" ...>
This works the same with fields_for as well
Offline
GarethAdams wrote:
Rails actually has this functionality built in already. Note the [] in the form_for call:
<%
conversation = Conversation.find(21) # could be assigned e.g. in a loop
form_for "conversation[]", conversation do |f| %>
<%= f.text_field :message %>
<% end %>
leads to
<input name="conversation[21][message]" ...>
This works the same with fields_for as well
Cool, I never knew this! I'll update the tutorial. Thanks.
Offline
Ive started my project over and for the life of me, can't get the update method to work! I've looked over the code at least 20 times, comparing it with the tutorial--no dice. whats up?
Last edited by darrenemo (2006-11-12 04:19:21)
Offline
nevermind, i found the update method from my previous app:
Offline
So everything is going good--i'm creating and editing memories, but my new.rhtml will also save conversations even if the message is blank. (each conversation has an author (conversation.author_id and a conversation.message)
how can I edit the
Last edited by darrenemo (2006-11-13 21:37:03)
Offline
Anyone got any ideas?
Offline
Sorry I've been away. I'm still planning on writing another tutorial for creating and editing models in the same form, but I haven't gotten around to it yet. Hopefully I'll have something up in a couple days if things go well.
Offline
Oh its all good Ryan--youve been such a great help so far though man. But what i'm asking has to do with the article up there, more specifically, the 'unless...' snippet
I just want to know how I can extract a column from the second model (@project.task.name) to use for comparison instead of values.all--but in my case what i need is @memory.conversation.message.. would you know to get that value each time it loops?
Offline
Try unless conversation[:message].blank?
Offline
This is cool, but how would you write a test for a controller action that updates multiple models? In other words, in an integration test, what would go in this spot:
post_via_redirect 'memory/update', { ? form elements for multiple models ?}
Offline
Take a look at the development log and see the parameters that are being passed to the action, this is what you should supply for your test. Something like this:
Offline
Great Tutorial!
I've seen examples where the index of existing elements use the id of that element, and the index of new elements are negative. I'm trying to adapt your writeup to utilize this schema, as it would solve some update issues.
-George
Last edited by harking (2007-01-29 17:23:12)
Offline