5

안녕하세요 저는 텍스트 필드에 입력 한 모든 이메일 쉼표 주소로 이메일을 보내고 싶습니다. 나는 그렇게하기 위해 컨트롤러를 사용한다. 쉼표로 구분 된 여러 이메일 주소로 이메일 보내기

def send_invites #friend invites 
    @emails_txt = params[:emails_txt] 
    @emails = @emails_txt.split(/\s*,\s*/) 
     @ve = [] 
     @emails.each do |email| 
     if email =~ /\b[A-Z0-9._%a-z-][email protected](?:[A-Z0-9a-z-]+.)+[A-Za-z]{2,4}\z/ 
      @ve << email 
     end 
     end 
@all_ok = true 
@errors = [] 

    @ve.each do |email| 

     @invitation = Invitation.new 
     @invitation.sender = current_user 
     @invitation.recipient_email = email 

     if @invitation.save 
     UserMailer.invitation_notification(current_user, @invitation, new_user_registration_url+"/#{@invitation.token}").deliver 
     else   
     @all_ok = "false" 
     @errors << @invitation.errors  
     end 
    end 
end 

그것은 잘 작동했지만 문제가 좀 도와 줘요 무슨

NoMethodError in InvitationsController#send_invites 
undefined method `username' for nil:NilClass 

을 다음과 같이 지금은 오류를주고있다. 감사합니다.

답변

3

이메일보기에서 nil 객체를 참조하는 것 같습니다.

UserMailer#invitation_controller 조치에 대한 귀하의 견해를보고 username을 참조하는 곳을 참조하십시오.

그런 다음 발견하면 개체를 추적하여 그 이유를 확인한 다음 그 주위에서 테스트를 추가합니다 (예 : if current_user.nil? ...).

+0

답장을 보내 주셔서 감사합니다. 그 쪽이 맞는 거 같아요. 전자 메일 주소 목록을 가져올 때 'current_user'가 nil이되지 않는 이유는 무엇인가요? –

+0

레일 대신 form_for 태그 대신 일반 html 양식을 사용했기 때문입니다. form_for 태그를 사용하면 폼에 보안 토큰과 관련하여 매우 중요한 응용 프로그램 토큰이 자동으로 포함됩니다. –