Ovidiu EFTIMIE
2006-Apr-07 08:06 UTC
[Rails] Validate_presence_of error in nested object is not displayed
Hi, I have a contact object . This contact has a address object>>Contactclass Contact < ActiveRecord::Base has_one :address validates_presence_of :first_name ,:last_name end .>>> Address class Address < ActiveRecord::Base belongs_to :contact validates_presence_of :address end Addres has the following fields ; address, phone, fax, email In the contacts_controller I have def create @contact = Contact.new(params[:contact]) @address = Address.new(params[:address]) @contact.address= @address @saved = @contact.save logger.debug @contact.address.errors if @saved list render :update do |page| page.replace_html "table", :partial=>"table" page.replace_html "pagination", :partial=>''pagination'' page.replace_html "msgs","Contact successfully added ",:queue=>''end'' page.visual_effect :fade,''operations'',:duration=>0.5 page.visual_effect :appear,''contentTable'' page.visual_effect :appear, ''msgs'',:queue=>''end'' page.visual_effect :fade, "msgs",:duration=>2,:queue=>''end'' end else render :update do |page| @form_name =''Adding contact'' page.replace_html ''formContent'',:partial=>''form'' end end end The problem is that the contact is saved and there is no message allerting me that the addres is mandatory . The line logger.debug @contact.address.errors shows me that accualy there is an error while saving but it''s not displayed . What I have tried to do is to save the contact then save the address @saved = @contact.save @savedAddr = @address.save If I do this the error message is displayed, but I''ve also noticed that the save is happening in two transactions Can anyone tell me how I can manage this ? Thanx, Ovidiu
Wiebe Cazemier
2006-Apr-07 11:00 UTC
[Rails] Re: Validate_presence_of error in nested object is not displayed
On Friday 07 April 2006 10:06, Ovidiu EFTIMIE wrote:> Hi, > I have a contact object . This contact has a address object >>>Contact > class Contact < ActiveRecord::Base > has_one :address > validates_presence_of :first_name ,:last_name > end > > .>>> Address > class Address < ActiveRecord::Base > belongs_to :contact > validates_presence_of :address > end > Addres has the following fields ; address, phone, fax, email > > In the contacts_controller I have > def create > @contact = Contact.new(params[:contact]) > @address = Address.new(params[:address]) > @contact.address= @address > @saved = @contact.save > logger.debug @contact.address.errors > if @saved > list > render :update do |page| > page.replace_html "table", :partial=>"table" > page.replace_html "pagination", :partial=>''pagination'' > page.replace_html "msgs","Contact successfully added ",:queue=>''end'' > page.visual_effect :fade,''operations'',:duration=>0.5 > page.visual_effect :appear,''contentTable'' > page.visual_effect :appear, ''msgs'',:queue=>''end'' > page.visual_effect :fade, "msgs",:duration=>2,:queue=>''end'' > end > else > render :update do |page| > @form_name =''Adding contact'' > page.replace_html ''formContent'',:partial=>''form'' > end > end > end > > The problem is that the contact is saved and there is no message > allerting me that the addres is mandatory . The line logger.debug > @contact.address.errors shows me that accualy there is an error while > saving but it''s not displayed . > What I have tried to do is to save the contact then save the address > @saved = @contact.save > @savedAddr = @address.save > If I do this the error message is displayed, but I''ve also noticed > that the save is happening in two transactions > > Can anyone tell me how I can manage this ? > > Thanx, > OvidiuI think if you use the pre-save method hook of contact to see if it has an unsaved object and save it would work. Also mind BTW, that when saving contact when the associated address is new will save the address, but when address is an existing altered object/record, then it will not be saved. You will have to take care of this in the pre-save hook as well. Or, in general cases, specify a custom association, for relations that a lot of objects have. The only problem is, is that the method you can check to see if a record is new (new_record?) returns false on an altered object (by design). That''s the whole cause of this behaviour te begin with. I don''t understand why rails only saves new associated objects, and not altered ones.
Reasonably Related Threads
- Rjs $("aaa").insertHtml is not a function error
- how does form_for and validate_presence_of work hand in hand
- Starting with RJS - not working
- RJS call from controller issues javascript that doesn''t get evaluated by browser
- validate_presence_of not working for me