我正在尝试制作一个简单的联系我们表格,因此我需要实现邮件程序。
我创建了一个控制器:
class ContactusController < ApplicationController
def index
@contact_us = Contactus.new
end
def new
redirect_to(action: 'index')
end
def create
@contact_us = Contactus.new(params[:contactus])
if @contact_us.deliver
flash[:notice] = "Thank-you, We will contact you shortly."
else
flash[:error] = "Oops!!! Something went wrong. Your mail was not sent."
end
render :index
end
end
因为我不想保存我使用 ActiveModel 的数据:
class Contactus
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActionView::Helpers::TextHelper
attr_accessor :name, :email, :message
validate :name,
:presence => true
validates :email,
:format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :message,
:length => { :minimum => 10, :maximum => 1000 }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def deliver
return false unless valid?
mail(
:to => "XXXXXXXX@gmail.com",
:from => %("#{name}" <#{email}>),
:reply_to => email,
:subject => "Website inquiry",
:body => message,
:html_body => simple_format(message)
)
true
end
def persisted?
false
end
end
一切正常。路由很好,验证有效,但我得到的唯一错误是:undefined method mail for #<Contactus:0x007f9da67173e8>
我试图在其中制作名称为 Contactus 和用户模型代码的邮件程序,但出现以下错误:private method new used
如何在 ActiveModel 中使用 ActionMailer 功能?