私はRuby on Railsアプリケーションでユーザーのデータベースを作成しましたが、いつでもデータベース内のすべてのユーザーに電子メールを送信するメーラーを作成しようとしています。Mailer: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
を認識していないようですが、私はその場所を与えています。このエラーを修正する方法の手掛かりは非常に高く評価されます。
はい、ありがとうございました。 – Mark