调用方法ruby时出现服务器错误500

时间:2016-09-23 06:27:33

标签: ruby ruby-on-rails-3

当我调用一个函数时,我得到以下错误日志; 请帮助破译它。

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'

编辑1:

匹配邮件的前几行:

class MatchingMailer < ActionMailer::Base
  default from: "\"Estimate My Project\" <info@estimatemyproject.com>"
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

1 个答案:

答案 0 :(得分:0)

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

表示first上没有方法Matching

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

表示您尝试在`app / mailers / matching_mailer.rb``的第6行匹配实例上调用方法first

查看第6行中的MatchingMailer,我们会在first上看到您致电@matching的人。 @matching之前只是设置了一行。请注意,Matching.find在传入单个id时返回一条记录,并在传入id数组时返回一组记录。在这种情况下,您将matchings作为参数提供给new_matchings_for_customer方法。

很明显,matchings参数必须是单个id。否则@matchings将返回一个数组,一个数组将响应first。由于您总是先调用并且从不关心数组中的其他值,因此只需加载一条记录就更有意义了。

将您的MatchingMailer更改为:

class MatchingMailer < ActionMailer::Base
  default from: '"Estimate My Project" <info@estimatemyproject.com>'

  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