Ruby on Rails 앱에서 사용자 데이터베이스를 만들었으므로 언제든지 데이터베이스의 모든 사용자에게 이메일을 보내는 메일러를 만들려고합니다. 여기 메일러 : Ruby on Rails를 사용하여 이메일을 보내지 못했습니다.
class MailMessage < ActionMailer::Base
def contact(recipient, subject, message)
# host = Hobo::Controller.request_host
# app_name = Hobo::Controller.app_name || host
@subject = subject
# @body = { :user => user, :host => host, :app_name => app_name }
@body["title"] = 'This is title'
@body["email"] = '[email protected]'
@body["message"] = message
@recipients = recipient
@from = '[email protected]'
@sent_on = Time.now
@headers = {}
end
end
내 컨트롤러 : 여기
내 모델의
다음
class MailMessageController < ApplicationController
def sendmail
email = @params["email"]
recipient = email["recipient"]
subject = email["subject"]
message = email["message"]
MailMessage.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => 'Message sent successfully'
end
def index
render :file => 'app/views/mail_message/index.html'
end
end
이야 내 views/mail_message
:
<h1>Send Email</h1>
<%= form_tag :action => 'sendmail' %>
<p>
<label for="email_subject">Subject</label>
<%= text_field 'email', 'subject' %>
</p>
<p>
<label for="email_recipient">Recipient</label>
<%= text_field 'email', 'recipient' %>
</p>
<p>
<label for="email_message">Message</label>
<%= text_area 'email', 'message' %>
</p>
<%= submit_tag "Send" %>
<%= form_tag %>
입니다 내 enviroment.rb
:
, 나는 다음과 같은 오류가 발생합니다 :
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
app/controllers/mail_message_controller.rb:4:in `sendmail'
그것은 sendmail
을 인식하지 않는 것,하지만 난 그 위치를 부여했다. 이 오류를 수정하는 방법에 대한 모든 단서는 매우 감사하겠습니다. 이 양식의 데이터 일에서 @를 삭제하기위한 것 인 경우
@params["email"]
:이 라인과 같은
예, 이제는 정상적으로 작동합니다. – Mark