2011-11-10 110 views
0

rails 3 appis正在使用create!方法创建一个控制器。调试创建!方法里面的控制器创建方法

有时它有效,有时它不会。它始终以相同的用例失败,但我已检查并重新检查,无法理解为什么失败。

创建!方法无提示失败,没有迹象表明问题的日志。我怎样才能创造!方法更详细?

代码:

class NotificationPaiementsController < ApplicationController 
    protect_from_forgery :except =>[:create] 
    skip_before_filter :authorize, :only => [:create] 

    def create 
    logger.debug "params is #{params}" 
    logger.debug "invoice is #{params[:invoice]}" 
    logger.debug "payment_status is #{params[:payment_status]}" 

    logger.debug "txn_id is #{params[:txn_id]}" 

    @notification_paiement = NotificationPaiement.create!(:params => params, 
               :cart_id => params[:invoice], 
               :status=> params[:payment_status], 
               :transaction_id => params[:txn_id]) 

    logger.debug "notification_paiement is #{@notification_paiement}" 
    render :nothing=>true 
    end 
end 

编辑: THX你的答案,它会一直快赶上例外,但我设法找出使用新savevia控制台的问题。在保存时,我有一个关于UTF-8编码的错误:ArgumentError:UTF-8中的无效字节序列。 Paypal正在改变“moli \ xE8re”中的“molière”,并且从未显示错误。

+0

可以粘贴你的' NotificationPaiement'模型和一个对'NotificationPaiementsController#create'的示例调用? – fuzzyalej

回答

1

create! constructor如果失败将引发异常

begin 
    @notification_paiement = NotificationPaiement.create!(... 
rescue ActiveRecord::RecordInvalid => e 
    # Deal with your errors. 
end 
1

在吞食异常信息的情况下,您可以临时删除config/initializers/backtrace_silencers.rb中的回溯消音器。

Creates an object just like Base.create but calls save! instead of save so an exception is raised if the record is invalid.

所以,如果你要使用create!,你应该把它包在异常处理: