2016-09-23 2 views
-1

함수를 호출 할 때 다음 오류 로그가 표시됩니다. 해독을 도와주세요.루비 메소드 호출시 서버 오류 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' 

당신이 라인 6에서 MatchingMailer 보면서`응용 프로그램/우편물/matching_mailer.rb``

라인 6 일치의 인스턴스에 방법 first를 호출하려고 의미, 우리는 모자를 참조 에 first으로 전화하십시오. @matching은 이전의 행으로 설정되었습니다. Matching.findid 하나를 전달하면 하나의 레코드를 반환하고 id 배열을 전달할 때 레코드 배열을 반환합니다. 이 경우 new_matchings_for_customer 메서드에 인수로 제공된 matchings을 전달합니다.

matchings 인수는 id이어야합니다. 그렇지 않으면 @matchings은 배열을 반환하고 배열은 first에 응답합니다. 항상 먼저 호출하고 배열의 다른 값을 신경 쓰지 않으므로 하나의 레코드를로드하는 것이 좋습니다.

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을 통과 할 수 있도록 :

MatchingMailer에 변경

.

+0

: 일치하는 메일러가 추가되었습니다. –

+0

나는 당신의 메일러를 알게되었으므로 나의 대답을 업데이트했다 ... – spickermann