Hi all, I''m having some issues to create a nested form with Rails3, the fields for creating my "children" object are not displayed in the form of its parent form. The code below is working under rails2 and not rails3, after reading the rails3 release note, they didn''t make any changes about nested attributes. So it should work, or maybe I''ve missed something... #model/book.rb class Book < ActiveRecord::Base has_one :author accepts_nested_attributes_for :author end #model/author.rb class Author < ActiveRecord::Base belongs_to :book end #views/books/_form.html.erb <%= form_for(@book) do |f| %> ... <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <!-- For some reasons this block doesn''t show up under rails3 but in rails 2 it''s working --> <% f.fields_for :author do |author_form|%> <div class="field"> <%= author_form.label :name%> <%= author_form.text_field :name %> </div> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %> #controllers/books_controller.rb def new @book = Book.new @book.build_author respond_to do |format| format.html # new.html.erb format.xml { render :xml => @book } end end I would appreciate any help, thanks. And I apologize if I made any english errors. Nico. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Solrac Caritas
2010-Oct-04 08:38 UTC
Re: Rails2/Rails3 nested form, fields_for not displayed
> <!-- For some reasons this block doesn''t show up under rails3 > but in rails 2 it''s working --> > <% f.fields_for :author do |author_form|%> > <div class="field"> > <%= author_form.label :name%> > <%= author_form.text_field :name %> > </div> > <% end %>replace <% f.fields_for :author do |author_form|%> with <%= f.fields_for :author do |author_form|%> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Not used Rails 3 yet, but shouldn''t your symbol be plural (:authors) since you have a one-to-many relationship? On Oct 4, 12:39 am, Nicolas <nico.ta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I''m having some issues to create a nested form with Rails3, the fields > for creating my "children" object are not displayed in the form of its > parent form. > > The code below is working under rails2 and not rails3, after reading > the rails3 release note, they didn''t make any changes about nested > attributes. So it should work, or maybe I''ve missed something... > > #model/book.rb > class Book < ActiveRecord::Base > has_one :author > accepts_nested_attributes_for :author > end > > #model/author.rb > class Author < ActiveRecord::Base > belongs_to :book > end > > #views/books/_form.html.erb > <%= form_for(@book) do |f| %> > ... > <div class="field"> > <%= f.label :title %><br /> > <%= f.text_field :title %> > </div> > > <!-- For some reasons this block doesn''t show up under rails3 > but in rails 2 it''s working --> > <% f.fields_for :author do |author_form|%> > <div class="field"> > <%= author_form.label :name%> > <%= author_form.text_field :name %> > </div> > <% end %> > > <div class="actions"> > <%= f.submit %> > </div> > <% end %> > > #controllers/books_controller.rb > def new > @book = Book.new > @book.build_author > > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @book } > end > end > > I would appreciate any help, thanks. And I apologize if I made any > english errors. > Nico.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It''s working. thanks a lot! It was a stupid error from me :). On Oct 4, 1:38 am, Solrac Caritas <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > <!-- For some reasons this block doesn''t show up under rails3 > > but in rails 2 it''s working --> > > <% f.fields_for :author do |author_form|%> > > <div class="field"> > > <%= author_form.label :name%> > > <%= author_form.text_field :name %> > > </div> > > <% end %> > > replace > <% f.fields_for :author do |author_form|%> > with > <%= f.fields_for :author do |author_form|%> > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris Miller
2010-Nov-21 02:59 UTC
Re: Rails2/Rails3 nested form, fields_for not displayed
Nicolas, Sorry to drag up this old topic, but I''m having the exact same problem. So what was your error? I assume I''m making the same mistake. Thanks in advance, Chris -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Solrac Caritas wrote in post #945648:> <% f.fields_for :author do |author_form|%> > with > <%= f.fields_for :author do |author_form|%>Thanks you very much It help me too! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.