有一个联系页面,提供输入姓名、电话、电子邮件和消息,然后发送到管理员的电子邮件。没有理由将消息存储在数据库中。
问题。如何:
在控制器中使用 Rails 验证,根本不使用模型,或者
在模型中使用验证,但没有任何数据库关系
升级版:
模型:
class ContactPageMessage
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :telephone, :email, :message
validates :name, :telephone, :email, :message, presence: true
validates :email, email_format: { :message => "Неверный формат E-mail адреса"}
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
控制器:
def sendmessage
cpm = ContactPageMessage.new()
if cpm.valid?
@settings = Setting.first
if !@settings
redirect_to contacts_path, :alert => "Fail"
end
if ContactPageMessage.received(params).deliver
redirect_to contacts_path, :notice => "Success"
else
redirect_to contacts_path, :alert => "Fail"
end
else
redirect_to contacts_path, :alert => "Fail"
end
end
end