我对Rails比较陌生。我使用Ruby 2.5和Rails 5.2创建了一个简单的应用程序,当网站访问者请求某些信息时,该应用程序将向管理员发送电子邮件。该应用程序不使用数据库,因为不会保存或存储任何访客信息,因此不使用ActiveRecord。只需通过表单从访问者那里获取一些信息并通过电子邮件发送即可。研究建议改为使用ActiveModel,但我仍然无法使其正常工作。无需第三方发送邮件应用程序。我在俯视什么?型号,控制器,视图,邮件程序代码显示在下面。
#Model: Visitor.rb
class Visitor
include ActiveModel::Model
include ActiveModel::Conversion
extend ActiveModel::Naming
include ActiveModel::Validations
attr_accessor :fname, :lname, :phone, :email, :interest
# Validations & Requirements
validates :fname, presence: true, length: { maximum: 50 }
validates :lname, presence: true, length: { maximum: 50 }
validates :lname, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
validates :interest, presence: true
def initialize(fname, lname, phone, email, interest)
@fname = fname
@lname = lname
@phone = phone
@email = email
@interest = interest
post_initialize
end
# #make in lieu of #create
def self.make(fname, lname, phone, email, interest)
attributes = {fname: fname, lname: lname, phone: phone, email: email, interest: interest}
object = new(attributes)
object.save
@visitor = object
@visitor
end
def persisted?
false
end
def send_mail
# Following method defined in NotifierMailer
NotifierMailer.visitor_info(self).deliver_now
end
private
def post_initialize
if email
email.downcase
end
end
end
# Controller: Visitors_controller.rb
class VisitorsController < ApplicationController
def new
@Visitor = Visitor.new
end
def make
@visitor = Visitor.new(visitor_params)
if @visitor.valid?
@visitor.send_mail # Instance method in Guest model
flash[:notice] = "Your request has been sent! Thank you for contacting us."
redirect_to welcome_path
else
render 'new'
end
end
private
def visitor_params
params.require(:visitor).permit(:fname, :lname, :phone, :email, :interest)
end
end
# Routes: Routes.rb
Rails.application.routes.draw do
get 'favicon', to: 'pages#favicon'
root to: 'pages#index', as: 'welcome'
# ... other routes for static page navigation
get '/reqinfo', to: 'visitors#new'
post '/reqinfo', to: 'visitors#make'
end
# View: New.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with model: @visitor, url: reqinfo_path, local: true do |form| %>
<%= render 'layouts/error_messages', object: form.object %>
<%= form.label 'First Name' %>
<%= form.text_field :fname, class: 'form-control' %>
<%= form.label 'Last Name' %>
<%= form.text_field :lname, class: 'form-control' %>
<%= form.label :phone %>
<%= form.text_field :phone, class: 'form-control' %>
<%= form.label 'Email Address' %>
<%= form.text_field :email, class: 'form-control' %>
<%= form.label 'Interest' %>
<%= form.text_field :interest, class 'form-control' %>
<%= form.submit 'Submit', id: "button" %>
<% end %><!-- form_with -->
</div><!-- col-md6-3 -->
</div><!-- row -->
# Mailer: Notifier_mailer.rb
class NotifierMailer < ApplicationMailer
default from: 'noreply@thecompany.com'
def guest_info(visitor)
@visitor = visitor
mail(to: 'admin@thecompany.com', subject: "Website Visitors Request for Information")
end
end
# Message: Visitor_info.html.erb
<h1>Dear Admin;</h1>
<br>
<% timestamp = Time.now %>
<p>
<%= visitor.fname %> <%= @visitor.lname %> visited the TheCompany website on <%= timestamp.localtime %>. He/she is requesting information regarding a <%= @visitor.interest %>. Their contact information phone: <%= @visitor.phone %> and email: <%= @visitor.email %>
</p>