Hi all, I''ve got two tables in my database, Book and Author. A book can have many authors, so I have my models set up as follows: Book: has_many :authors Author: belongs_to :book Simple enough, right? I can access book.authors and get all of the authors for the book, everyone''s happy and Rails is a wonderful thing. However, what I''m wondering, is if there''s an easy shortcut for setting up the edit form and the update action? Ideally what I''d like to do in the view is: <% @book.authors.each do |author| %> <tr> <td>Author First Name:</td> <td><%= text_field "book", "SOMETHING", "size" => 20 %></td> </tr> <tr> <td>Author Last Name:</td> <td><%= text_field "book", "SOMETHING", "size" => 20 %></td> </tr> <% end %> and then in the update action just call @book.update_attributes(params[''source'']) and have it automatically update the book as well as all of the authors submitted for the book. Is that even possible. I know it wouldn''t be hard to process the data myself, but I''m new at Rails and I''d hate to start writing lots of code when it turns out there''s a 1 line solution ;). Thanks in advance, Joshua
> and then in the update action just call > @book.update_attributes(params[''source'']) and have it > automatically update the book as well as all of the authors > submitted for the book.Try this: <% @book.authors.each do |author| %> <tr> <td>Author First Name:</td> <td><%= text_field "author[]", "SOMETHING", "size" => 20 %></td> </tr> <tr> <td>Author Last Name:</td> <td><%= text_field "author[]", "SOMETHING", "size" => 20 %></td> </tr> <% end %> And in the update add the line: Author.update(params[:author].keys, params[:author].values) -- Erick Sasse www.ericksasse.com.br _______________________________________________________ Promoo Yahoo! Acesso Grtis: a cada hora navegada voc acumula cupons e concorre a mais de 500 prmios! Participe! http://yahoo.fbiz.com.br/
That didn''t work, I get this error On the reference to author[]: NoMethodError in Book#edit WARNING: You have a nil object when you probably didn''t expect it! Odds are you want an instance of ActiveRecord::Base instead. It''s not a big deal, I can definitely get it working, I was just wondering if there was a more "Rails" way to accomplish it, so to speak. On 10/9/05, Erick Sasse <esasse-/E1597aS9LRfJ/NunPodnw@public.gmane.org> wrote:> > > and then in the update action just call > > @book.update_attributes(params[''source'']) and have it > > automatically update the book as well as all of the authors > > submitted for the book. > > Try this: > > <% @book.authors.each do |author| %> > <tr> > <td>Author First Name:</td> > <td><%= text_field "author[]", "SOMETHING", "size" => 20 %></td> > </tr> > <tr> > <td>Author Last Name:</td> > <td><%= text_field "author[]", "SOMETHING", "size" => 20 %></td> > </tr> > <% end %> > > And in the update add the line: > > Author.update(params[:author].keys, params[:author].values) > > -- > Erick Sasse > www.ericksasse.com.br > > > > > > > > _______________________________________________________ > Promoção Yahoo! Acesso Grátis: a cada hora navegada você acumula cupons e concorre a mais de 500 prêmios! Participe! http://yahoo.fbiz.com.br/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
ok Erick, I saw your question about displaying validation methods and I noticed you had: <% @tipo = tipo -%> inside the loop. I added <% @author = author - %> and now it works like a charm. I''m not exactly clear on what the - operator is doing here though? On 10/9/05, Joshua Cohen <defeated-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That didn''t work, I get this error On the reference to author[]: > > NoMethodError in Book#edit > > WARNING: You have a nil object when you probably didn''t expect it! > Odds are you > want an instance of ActiveRecord::Base instead. > > It''s not a big deal, I can definitely get it working, I was just > wondering if there was a more "Rails" way to accomplish it, so to > speak. > > On 10/9/05, Erick Sasse <esasse-/E1597aS9LRfJ/NunPodnw@public.gmane.org> wrote: > > > > > and then in the update action just call > > > @book.update_attributes(params[''source'']) and have it > > > automatically update the book as well as all of the authors > > > submitted for the book. > > > > Try this: > > > > <% @book.authors.each do |author| %> > > <tr> > > <td>Author First Name:</td> > > <td><%= text_field "author[]", "SOMETHING", "size" => 20 %></td> > > </tr> > > <tr> > > <td>Author Last Name:</td> > > <td><%= text_field "author[]", "SOMETHING", "size" => 20 %></td> > > </tr> > > <% end %> > > > > And in the update add the line: > > > > Author.update(params[:author].keys, params[:author].values) > > > > -- > > Erick Sasse > > www.ericksasse.com.br > > > > > > > > > > > > > > > > _______________________________________________________ > > Promoção Yahoo! Acesso Grátis: a cada hora navegada você acumula cupons e concorre a mais de 500 prêmios! Participe! http://yahoo.fbiz.com.br/ > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >