HighDots Forums  

Error Messages while entering data in 2 tables with one form

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss Error Messages while entering data in 2 tables with one form in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Salil Gaikwad
 
Posts: n/a

Default Error Messages while entering data in 2 tables with one form - 07-03-2009 , 10:15 AM






Hi All,

I want to enter parameters in 2 tables which are submitted using one
form.
Following is the relation of models.
In UserInfo.rb
has_one :user
validates_associated :user
validates_presence_of :city

In User.rb
belongs_to :user_info
validates_presence_of :first_name


I write following code in my UserInfosController
@user_info = UserInfo.new(params[:user_info])
@user_info.build_user(:first_name=>'') # which i'll modify later
@user_info.save

now if i click the submit button w/o entering any values i.e.
params[:userinfo][:city]=""

it gives me two error
* City can't be blank
* User is invalid


but i want second error should be as follows
*first name can't be blank

I would like to know how to do it or is there any other way in rails to
enter data into two tables using one form.


Regards,

Salil Gaikwad
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #2  
Old   
Gavin
 
Posts: n/a

Default Re: Error Messages while entering data in 2 tables with oneform - 07-03-2009 , 10:27 AM






validates_associated checks if the associated object (in this case
User) is a valid record.
If not, it adds an error to the UserInfo object's user method. ("User
is invalid")

"first name can't be blank" is an error on the User object's
first_name method.

If you want to display the "first name can't be blank" you'll need to
access it either through <%= error_messages_for :user %> or
@user.errors.on(:first_name)

Hope that helps

Gavin

http://handyrailstips.com

On Jul 3, 3:15*pm, Salil Gaikwad <rails-mailing-l... (AT) andreas-s (DOT) net>
wrote:
Quote:
Hi All,

I want to enter parameters in 2 tables which are submitted using one
form.
Following is the relation of models.
In UserInfo.rb
has_one :user
validates_associated :user
validates_presence_of :city

In User.rb
belongs_to :user_info
validates_presence_of :first_name

I write following code in my UserInfosController
@user_info = UserInfo.new(params[:user_info])
@user_info.build_user(:first_name=>'') *# which i'll modify later
@user_info.save

now if i click the submit button w/o entering any values i.e.
*params[:userinfo][:city]=""

it gives me two error
* * * City can't be blank
* * * User is invalid

but i want second error should be as follows
* * *first name can't be blank

I would like to know how to do it or is there any other way in rails to
enter data into two tables using one form.

Regards,

Salil Gaikwad
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #3  
Old   
Salil Gaikwad
 
Posts: n/a

Default Re: Error Messages while entering data in 2 tables with oneform - 07-03-2009 , 11:00 AM



Hi Gavin,


I try following as u say
<%= error_messages_for :user %>
but it gives me same error messages.
i don't know how to use @user.errors.on(:first_name)
and where to use?


Regards,

Salil Gaikwad
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #4  
Old   
Gavin
 
Posts: n/a

Default Re: Error Messages while entering data in 2 tables with oneform - 07-04-2009 , 05:43 AM



To get rid of the "user is invalid", remove
validates_associated :user from the UserInfo model

Meanwhile, read up on validates_associated:
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002461
and make sure you want ot use it here.

Gavin

http://handyrailstips.com

On Jul 3, 4:00*pm, Salil Gaikwad <rails-mailing-l... (AT) andreas-s (DOT) net>
wrote:
Quote:
Hi Gavin,

I try following as u say
%= error_messages_for :user %
*but it gives me same error messages.
i don't know how to use @user.errors.on(:first_name)
and where to use?

Regards,

Salil Gaikwad
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #5  
Old   
Gavin
 
Posts: n/a

Default Re: Error Messages while entering data in 2 tables with oneform - 07-04-2009 , 05:55 AM



To get rid of the "user is invalid", remove
validates_associated :user from the UserInfo model

Meanwhile, read up on validates_associated:
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002461
and make sure you want ot use it here.

Gavin

http://handyrailstips.com

On Jul 3, 4:00*pm, Salil Gaikwad <rails-mailing-l... (AT) andreas-s (DOT) net>
wrote:
Quote:
Hi Gavin,

I try following as u say
%= error_messages_for :user %
*but it gives me same error messages.
i don't know how to use @user.errors.on(:first_name)
and where to use?

Regards,

Salil Gaikwad
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.