class Shop < ActiveRecord::Base has_many :documents validates :name, :presence => true accepts_nested_attributes_for :documents class Document < ActiveRecord::Base belongs_to :shop validates :reference_number, :presence => true The partial _form for shop is: - @shop.documents.build = simple_form_for(@shop) do |shop_f| = render ''shared/error_messages'', :object => @shop = render :partial => ''documents/form'', :locals => { :form => shop_f } = field_set_tag t(''shop'') do .inputs = shop_f.input :role_number = shop_f.input :role_date = shop_f.input :name the partial _form for document is: = form.simple_fields_for :documents do |document_f| = field_set_tag t(''document'') do .inputs = document_f.input :reference_number When I submit the form with no values in shop name or document reference_number I have the errors messages and it is correct but in the page I have the shop form with two document form. Why the two document forms? Sorry for my bad english hope my problem is clear. -- 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.
On May 6, 10:42 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Shop < ActiveRecord::Base > has_many :documents > validates :name, :presence => true > accepts_nested_attributes_for :documents > > class Document < ActiveRecord::Base > belongs_to :shop > validates :reference_number, :presence => true >So your shop starts out as a brand new shop on in the new action, with 0 shops. The new form gets rendered,> The partial _form for shop is: > > - @shop.documents.buildthe shop now has 1 document. You then submit the form, which creates a shop with one document, (albeit with errors). because there are errors, you render the form, and since the form unconditionally does @shop.documents.build, a second document is created. If I were you I''d be calling that in the controller, and only when desirable Fred> = simple_form_for(@shop) do |shop_f| > = render ''shared/error_messages'', :object => @shop > > = render :partial => ''documents/form'', :locals => { :form => shop_f } > > = field_set_tag t(''shop'') do > .inputs > = shop_f.input :role_number > = shop_f.input :role_date > = shop_f.input :name > > the partial _form for document is: > > = form.simple_fields_for :documents do |document_f| > = field_set_tag t(''document'') do > .inputs > = document_f.input :reference_number > > When I submit the form with no values in shop name or document > reference_number I have the errors messages and it is correct but in > the page I have the shop form with two document form. > Why the two document forms? > Sorry for my bad english hope my problem is clear.-- 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.
On 6 May 2011 23:53, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On May 6, 10:42 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> class Shop < ActiveRecord::Base >> has_many :documents >> validates :name, :presence => true >> accepts_nested_attributes_for :documents >> >> class Document < ActiveRecord::Base >> belongs_to :shop >> validates :reference_number, :presence => true >> > > So your shop starts out as a brand new shop on in the new action, with > 0 shops. > > The new form gets rendered, > >> The partial _form for shop is: >> >> - @shop.documents.build > > the shop now has 1 document. You then submit the form, which creates a > shop with one document, (albeit with errors). because there are > errors, you render the form, and since the form unconditionally does > @shop.documents.build, a second document is created. > If I were you I''d be calling that in the controller, and only when > desirable >Oh great, thank you very much I were going crazy. Yes I put @shop.document.build in the controller now. -- 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.