2016-09-23 45 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

相关问题