2016-09-23 46 views
-1

當我調用一個函數時,我得到以下錯誤日誌; 請幫助解密它。調用方法ruby時出現服務器錯誤500

NoMethodError (undefined method `first' for #<Matching:0x0000000875a050>): 
    app/mailers/matching_mailer.rb:6:in `new_matchings_for_customer' 
    app/models/matching.rb:133:in `block in create_matchings_from_service' 
    app/models/matching.rb:126:in `each' 
    app/models/matching.rb:126:in `create_matchings_from_service' 
    app/models/matching.rb:30:in `process_matchings_for_service' 
    app/models/payments/subscription.rb:94:in `find_matchings' 
    app/models/payments/subscription.rb:85:in `after_create_actions' 
    app/controllers/contractors/subscriptions_controller.rb:51:in `subscribe' 
    app/controllers/contractors/subscriptions_controller.rb:19:in `create' 

EDIT 1:

第一匹配郵包的幾行:

class MatchingMailer < ActionMailer::Base 
    default from: "\"Estimate My Project\" <[email protected]>" 
def new_matchings_for_customer(matchings, customer_id) 
    @customer = Customer.find(customer_id) 
@matchings = Matching.find(matchings) 
@category = @matchings.first.emp_request.subcategory.category 
unless @customer.email.empty? 
    mail(to: @customer.email, subject: "#{@category.name} estimate for project in #{@customer.zip_code.county.name}, #{@customer.zip_code.state.code} #{@customer.zip_code.code}") 
else 
    self.message.perform_deliveries = false 
end 
end 

回答

0
NoMethodError (undefined method `first' for #<Matching:0x0000000875a050>) 

意味着有一個Matching沒有方法first

app/mailers/matching_mailer.rb:6:in `new_matchings_for_customer' 

意味着你嘗試調用方法first上的實例在'應用程序/郵寄/ matching_mailer.rb``

的6號線匹配6行看你MatchingMailer,我們看到帽子你可撥打電話first,電話號碼@matching@matching被設置在前一行。請注意,當您傳遞一個id時,Matching.find會返回一條記錄,並在您傳遞一組id時返回一組記錄。在這種情況下,您將matchings作爲參數提供給new_matchings_for_customer方法。

很明顯,matchings參數必須是單個id。否則@matchings將返回一個數組,並且數組將響應first。既然你總是先調用,而不關心數組中的其他值,那麼加載一條記錄更有意義。

更改MatchingMailer到:

class MatchingMailer < ActionMailer::Base 
    default from: '"Estimate My Project" <[email protected]>' 

    def new_matchings_for_customer(matching_id, customer_id) 
    customer = Customer.find(customer_id) 

    if customer.email.present? 
     matching = Matching.find(matching_id) 
     category = matching.emp_request.subcategory.category 

     mail(
     to: customer.email, 
     subject: "#{category.name} estimate for project in #{customer.zip_code.county.name}, #{customer.zip_code.state.code} #{customer.zip_code.code}" 
    ) 
    else 
     self.message.perform_deliveries = false 
    end 
    end 
end 

,並保證只調用該方法時傳遞一個matching_id

+0

:添加了我的匹配郵件行。 –

+0

我現在更新了我的答案,我知道你的郵件... – spickermann

相關問題