J'ai la méthode suivante pour créer un contrôleur de contact:Comment créer un rappel qui s'exécute après l'enregistrement final dans la méthode create dans Rails?
def create
puts "in create method"
@contact = Contact.create(params[:contact])
unless @contact.vcard.path.blank?
paperclip_vcard = File.new(@contact.vcard.path)
@vcard = Vpim::Vcard.decode(paperclip_vcard).first
@contact.title = @vcard.title
@contact.email = @vcard.email
@contact.first_name = @vcard.name.given
@contact.last_name = @vcard.name.family
@contact.phone = @vcard.telephone
@contact.address.street1 = @vcard.address.street
@contact.address.city = @vcard.address.locality
@contact.address.state = @vcard.address.region
@contact.address.zip = @vcard.address.postalcode
@contact.company_name = @vcard.org.fetch(0)
end
@contact.user_id = current_user.id # makes sure every new user is assigned an ID
if @contact.save
#check if need to update company with contact info
@contact.update_company
flash[:notice] = "Successfully created contact."
redirect_to @contact
else
render :action => 'new'
end
end
Le rappel que je veux courir devrait être fait après la ligne if @contact.save
à la fin de la méthode ... mais en ce moment, il l'exécute dans le premier .create.
Comment puis-je permettre à un rappel de s'exécuter seulement après que tout le traitement dans la méthode de création est terminé?
Utilisez .new au lieu de .create. @contact = Contact.new (params [: contact]) –