Hi
I have 2 models like request and contacts
Request < ActiveRecord::Base
has_many :contacts
end
Contact < ActiveRecord::Base
belongs_to :request
validates_presence_of :name, :message => ''Name should not be
blank''
validates_presence_of :email, :message => ''Email should not be
blank''
end
Now in RequestController
def create
@request = Request.new(params[:request])
@request.contacts << Contact.new(params[:contact])
if @request.save
-------everything ok
else
self.flash[:error] = @request.errors.each{}
render :action => "new"
end
end
But here my problem is I expected
''Name should not be blank'' ''email should not
be blank''
But what I got is
contacts is invalid
Why this Please help me to get the errors as I expected
Thanks in advance
Sijo
--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hi
I did not get a solution to this yet.Could anybody please help?
Sijo
--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
flash[:error] = @request.errors.full_messages.join('', '')
On Fri, Feb 20, 2009 at 2:05 AM, Sijo Kg
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
> But here my problem is I expected
> ''Name should not be blank'' ''email should
not be blank''
>
> But what I got is
> contacts is invalid
>
> Why this Please help me to get the errors as I expected
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@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
-~----------~----~----~----~------~----~------~--~---
Hi
That is correct I get the same result when use
@request.errors.each{}
But my problem is when email and name blank, I dont get the error
message flash as
Name should not be blank
Email should not be blank
but I get error like
contacts is invalid
But if suppose my request model too has a validation like
validates_presence_of :reason, :message => ''Reason should not be
blank''
I get the whole flash[error] like
Reason should not be blank
contacts is invalid
Why the individual error messages of contact model not appearing?
Sijo
--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
You need to get the contact''s errors from the contact. Try this out:
@request = Request.new(params[:request])
@request.contacts << contact = Contact.new(params[:contact]
if @request.save
# everything''s fine
else
flash[:error] = @request.errors.full_messages.join('',
'') +
contact.errors.full_messages.join('', '')
end
On Mon, Feb 23, 2009 at 9:28 PM, Sijo Kg
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> Hi
> That is correct I get the same result when use
> @request.errors.each{}
> But my problem is when email and name blank, I dont get the error
> message flash as
>
> Name should not be blank
> Email should not be blank
>
> but I get error like
>
> contacts is invalid
>
> But if suppose my request model too has a validation like
> validates_presence_of :reason, :message => ''Reason should not
be blank''
>
> I get the whole flash[error] like
>
> Reason should not be blank
> contacts is invalid
>
> Why the individual error messages of contact model not appearing?
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@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
-~----------~----~----~----~------~----~------~--~---
No hope Again it shows contacts is invalid rather than the actual message what I expected Sijo -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi,
Try the following
@request.contacts.errors.each{}
--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Again error undefined method `errors'' for #<Class:0xb67d8848> -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sijo Kg wrote:> Again error > > undefined method `errors'' for #<Class:0xb67d8848>Just give contacts.errors.each{} -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
undefined local variable or method `contacts'' for #<RequestsController:0xb67de400> -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The only think I can think of is that you have a model variable named @request, which is also, by default, the name of the HTTP request object. There may be some weird overwriting going on. Rename your model. It will save you huge headaches in the long run, not least when trying to ask for help, since people will always assume that @request refers to the HTTP request. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi
I changed the name from @request to @companyrequest and the
corresponding model to CompanyRequest Still now problem exists..Is
anything wrong in the saving code?
@companyrequest = CompanyRequest.new(params[:companyrequest])
@companyrequest.contacts << Contact.new(params[:contact])
if @companyrequest.save
Sijo
--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---