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
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?
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.