我正在尝试按照this tutorial制作联系表单,但在提交表单后我仍然遇到错误:uninitialized constant ApplicationMailer
。
跟踪提供以下信息:
app/mailers/message_mailer.rb:1:in <top (required)>'
app/controllers/messages_controller.rb:9:in create'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in send_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:198:in process_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in process_action'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in block in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:555:in block (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:92:in __run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in _run_process_action_callbacks'`
以下是我的文件: 的控制器/ messages_controller.rb
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(message_params)
if @message.valid?
MessageMailer.message_me(@message).deliver_now
redirect_to new_message_path, notice: "Thankyou for your message."
else
render :new
end
end
private
def message_params
params.require(:message).permit(:name, :email, :subject, :content)
end
end
模型/ message.rb
class Message
include ActiveModel::Model
attr_accessor :name, :email, :subject, :content
validates :name, :email, :subject, :content, presence: true
end
寄件人/ message_mailer.rb
class MessageMailer < ApplicationMailer
default :to => "jd.levarato@gmail.com"
def message_me(msg)
@msg = msg
mail from: @msg.email, subject: @msg.subject, body: @msg.content
end
end
答案 0 :(得分:4)
您必须创建继承自ActionMailer :: Base。
的ApplicationMailer类application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'from@exmaple.com'
layout 'mailer'
end
或者您可以简单地将ActionMailer :: Base继承到MessageMailer
class MessageMailer < ActionMailer::Base
default :to => "jd.levarato@gmail.com"
def message_me(msg)
@msg = msg
mail from: @msg.email, subject: @msg.subject, body: @msg.content
end
end