我为Contact控制器提供了以下create方法:
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我想要运行的回调应该在method...but最末尾的if @contact.save行之后完成,它在第一个.create.中运行它
如何仅在create方法中的所有处理完成后才允许回调运行?
发布于 2010-11-04 03:43:38
基本上,ActiveRecord::Callback create(...) -创建一个对象(或多个对象)并将其保存到数据库,如果验证通过的话。无论对象是否成功保存到数据库,都会返回结果对象。
您需要使用(这来自您的脚本逻辑):
@contact = Contact.new(params[:contact])如果您想要回调保存,请使用after_save回调。
https://stackoverflow.com/questions/4090888
复制相似问题