On 26 Jul 2007, at 19:31, Bahadır Doğan wrote:> I prepared a form for adding records to the table. I want the model
> class to validate the uniqeuness of the name field but it doesn''t
give
> any errors for the duplicate record.
>
> Here is the controller :
>
> class PetController < ApplicationController
> def new
> check_authentication
> if request.post?
> begin
> @file = params[:pet_image]
> if @file.original_filename.length > 0
> pet = Pet.new
> #set the fields
> pet.save
> File.open(filename, "wb") { |f| f.write(@file.read)
}
> redirect_to(:controller=>''user'',
:action=>''petadmin'')
> end
> end
> end
> end
It looks like you''re not checking whether the model is actually
saving correctly. In the code above, pet.save will return false but
you''re not checking the returned value. You need to either use:
if pet.save
File.open(filename, "wb") { |f| f.write(@file.read) }
redirect_to(:controller=>''user'',
:action=>''petadmin'')
else
# Fallback action
end
or use pet.save! which will raise an exception if the model is invalid.
You might also want to look at using before_filter rather than
calling check_authentication in every method.
James.
--
James Stewart
Play: http://james.anthropiccollective.org
Work: http://jystewart.net/process/
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---