2016-12-21 2 views
1
rails 3.2 

、私は次のようしている:私のticket.rbモデルでRailsメソッドを実行するとき、実行しないでください。私tickets_controllerで

def update 
    @ticket = Ticket.find params[:id] 
    authorize! :update, @ticket 
    @ticket.assign_attributes(params[:ticket]) 
    @ticket.customer_info.company = @ticket.customer if @ticket.customer_info 
    @ticket.admin_context = true 
    if !params[:ticket_update_type].nil? && params[:ticket_update_type] == 'save_lead_billing' 
    @ticket.process_lead_billing params 
    end 
    if @ticket.save 
    @ticket.update_attribute(:ticket_type, @ticket.ticket_profile.ticket_type) 
    redirect_to [:admin, @ticket], notice: success_message 
    else 
    @ticket.customer_info_type = 'existing' 
    @can_update_ticket = can? :update, @ticket 
    @tag_groups = TagGroup.with_type('ticket').for_company(@ticket.customer_id) 
    flash.now[:error] = @ticket.errors.full_messages.join(', ') 
    render action: "show" 
    end 
end 

、私は次のようしている:

def process_lead_billing params 
    if params[:lead_billing]["pre_tax_total"].nil? || params[:lead_billing]["post_tax_total"].nil? 
    return 
    end 
    # handles case where billing infor has not been added to lead ticket 
    if params[:ticket_update_type] == "save_lead_billing" 
    lead_billing = LeadBilling.new(
     :ticket_id => self.id, 
     :pre_tax_total => params[:lead_billing]["pre_tax_total"], 
     :post_tax_total => params[:lead_billing]["post_tax_total"], 
     :status => 'entered' 
    ) 
    lead_billing.save! 
    end 

エンド

そしてlead_billing.rbでモデル、私は以下を持っています:

class LeadBilling < ActiveRecord::Base 
    validates_presence_of :pre_tax_total, :post_tax_total 
    validates_numericality_of :pre_tax_total, greater_than: 0, allow_blank: false, only_integer: false 
    validates_numericality_of :post_tax_total, greater_than_or_equal_to: :pre_tax_total, allow_blank: false, only_integer: false  

問題は、pre_tax_totalとpost_tax_totalを空にしてフォームを送信すると、エラーメッセージが表示されることです。

ログファイルから:

Started PUT "/admin/tickets/163812" for 73.83.66.151 at 2016-12-21 22:05:28 +0000 
Processing by Admin::TicketsController#update as HTML 

とのparamsは、次のとおりです。

[utf8] => ✓ 
[authenticity_token] => mNt+aI3YInoutup4UsBGZ8zZkeFRYCBZAsxEv4JPvoE= 
[ticket] => Array 
    (
     ...... 
    ) 

[time_span] => 
[city] => Draper 
[state] => Utah 
[admin] => true 
[specialty] => 
[services] => 
[inventories] => 
[ticket_update_type] => save_lead_billing 
[ticket_id] => 1480720184_0388234_ticket 
[lead_billing] => Array 
    (
     [pre_tax_total] => 
     [post_tax_total] => 
    ) 

[id] => 163812 

ログファイルから、エラーは次のとおりです。

Validation failed: Pre tax total can't be blank, Pre tax total is not 
a number, Post tax total can't be blank, Post tax total is not a number from  

そして、それは私を指しticket_controller.rb内のprocessing_lead_billingメソッドが呼び出された行(正しく)、次にproc内の行essing_lead_billingメソッドで、リード請求を保存しようとします。

実行を停止する必要がありましたが、nilをチェックしても実行は継続されました。何か案は?

+0

は、ファイル内の間違った場所を示すデバッガである可能性があります。そのメソッドに 'puts'やデバッガを投げ込み、実際に呼び出されているかどうか確認してください。 –

+3

内容がnilか空白(つまり "")の場合、あなたのパラメータからコピーしたことは明白ではありません。その場合、メソッドは返されません(ゼロの代わりに空白をテストする必要があります)。 – Nycen

+2

@Nycen:空ではなく空白を送っていた。そして、私はいくつかのテストをしました、そして、もし私がobject.blankのテストをすればそれが判明しましたか?それは空白とヌルの両方を対象としているので、代わりにそれを使用しています。うまくいきます。これを答えにすると、私はそれを信用します。 – EastsideDeveloper

答えて

1

Railsには空白がありますか?通常はフォームパラメータをテストするときに推奨されるメソッドです。

nil?オブジェクトがnilで空白の場合にのみtrueを返すルビメソッドです。あらゆる種類のユースケース(空の文字列、空の配列、空の辞書、もちろんゼロ値)をカバーします。

あなたの場合、戻り値はほとんどの場合、空の文字列であり、レールが動作するので、空白をテストする必要がありますか? nilの代わりに?