我为Contact控制器提供了以下创建方法:
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
我想要运行的回调应该在方法最后的if @contact.save
行之后完成...但是现在,它在第一个.create.
如果在完成create方法中的所有处理后,如何允许回叫才能运行?
答案 0 :(得分:1)
基本上,ActiveRecord :: Callback create(...) - 如果验证通过,则创建一个对象(或多个对象)并将其保存到数据库。无论对象是否已成功保存到数据库,都会返回结果对象。
您需要使用(这取决于您的脚本逻辑):
@contact = Contact.new(params[:contact])
如果要回调保存,请使用after_save回调。