我想做一個簡單的聯繫我們的形式,所以我需要實現郵件。ActionMailer和ActiveModel
我創建了一個控制器:
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\-][email protected](?:[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 => "[email protected]",
: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>
我試着與名稱聯繫我們,並在該用戶模型代碼郵件,但我得到了 以下錯誤:private method new used
我如何在ActiveModel中使用ActionMailer函數?
我會嘗試一定,關於新的行動問題,我發現我創建一個郵件類的對象,但事實上它沒有construtor所以有問題:) –
這將做到這一點;) –
現在我有點前進,但現在變得非常奇怪errro Net :: SMTPAuthenticationError即使我進入正確的東西在ActionMailer :: Base.smtp_settings enviornment.rb任何線索關於那 –