Gem นี้ถูกสร้างขึ้นบน ActiveModel
เพื่อแสดงให้เห็นว่าคุณสามารถดึงการตรวจสอบความถูกต้อง การตั้งชื่อ และ i18n
จาก Rails ไปยังโมเดลของคุณได้อย่างไร โดยไม่จำเป็นต้องดำเนินการทั้งหมดด้วยตัวเอง
README นี้หมายถึง MailForm gem ที่จะใช้ใน Rails 5+ สำหรับคำแนะนำเกี่ยวกับวิธีการใช้ MailForm ใน Rails เวอร์ชันเก่า โปรดดูที่สาขาที่มีอยู่
MailForm ช่วยให้คุณสามารถส่งอีเมลได้โดยตรงจากแบบฟอร์ม ตัวอย่างเช่น หากคุณต้องการสร้างแบบฟอร์มการติดต่อ เพียงกรอกบรรทัดต่อไปนี้ (รวมถึงอีเมลด้วย):
class ContactForm < MailForm :: Base
attribute :name , validate : true
attribute :email , validate : / A [^@ s ]+@[^@ s ]+ z /i
attribute :file , attachment : true
attribute :message
attribute :nickname , captcha : true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
subject : "My Contact Form" ,
to : "[email protected]" ,
from : %(" #{ name } " < #{ email } >)
}
end
end
จากนั้นคุณเริ่มคอนโซลด้วย rails console
และพิมพ์:
>> c = ContactForm . new ( name : 'José' , email : '[email protected]' , message : 'Cool!' )
>> c . deliver
ตรวจสอบกล่องจดหมายของคุณและอีเมลจะอยู่ที่นั่นพร้อมกับช่องที่ส่ง (สมมติว่าคุณกำหนดค่าวิธีการจัดส่งจดหมายของคุณอย่างถูกต้อง)
เมื่อคุณสืบทอดจาก MailForm::Base
มันจะดึงชุดของสิ่งต่าง ๆ จาก ActiveModel
เช่น ActiveModel::Validation
, ActiveModel::Translation
และ ActiveModel::Naming
สิ่งนี้จะนำ I18n
ข้อความแสดงข้อผิดพลาด การตรวจสอบความถูกต้อง และการจัดการแอตทริบิวต์เช่นเดียวกับใน ActiveRecord
มาสู่ MailForm ดังนั้น MailForm จึงสามารถใช้ในคอนโทรลเลอร์และเครื่องมือสร้างแบบฟอร์มของคุณได้โดยไม่ต้องปรับแต่งเพิ่มเติม นอกจากนี้ยังหมายความว่าแทนที่จะเป็นสิ่งต่อไปนี้:
attribute :email , validate : / A [^@ s ]+@[^@ s ]+ z /i
คุณสามารถทำได้จริง:
attribute :email
validates_format_of :email , with : / A [^@ s ]+@[^@ s ]+ z /i
เลือกอันที่คุณพอใจมากที่สุด สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ API โปรดอ่านต่อด้านล่าง
MailForm เล่นได้ดีกับ ORM เช่นกัน คุณเพียงแค่ต้องรวม MailForm::Delivery
ไว้ในโมเดลของคุณและประกาศว่าควรส่งแอตทริบิวต์ใด:
class User < ActiveRecord :: Base
include MailForm :: Delivery
append :remote_ip , :user_agent , :session
attributes :name , :email , :created_at
def headers
{
to : "[email protected]" ,
subject : "User created an account"
}
end
end
การส่งมอบจะถูกทริกเกอร์ใน after_create
hook
การติดตั้ง MailForm นั้นง่ายมาก เพียงแก้ไข Gemfile ของคุณโดยเพิ่มสิ่งต่อไปนี้:
gem 'mail_form'
จากนั้นรัน bundle install
เพื่อติดตั้ง MailForm
คุณสามารถเรียกใช้ rails generate mail_form
เพื่อดูข้อมูลความช่วยเหลือเกี่ยวกับวิธีสร้างแบบฟอร์มพื้นฐานเพื่อเริ่มต้นใช้งาน
ประกาศแอตทริบิวต์แบบฟอร์มของคุณ คุณลักษณะทั้งหมดที่ประกาศไว้ที่นี่จะถูกผนวกเข้ากับอีเมล ยกเว้นแอตทริบิวต์ :captcha เป็นจริง
ตัวเลือก:
:validate
- ตะขอสำหรับ validates_*_of
เมื่อกำหนดให้ true
ให้ตรวจสอบการมีอยู่ของแอตทริบิวต์ เมื่อ regexp ตรวจสอบรูปแบบ เมื่ออาร์เรย์ ตรวจสอบการรวมแอตทริบิวต์ในอาร์เรย์
เมื่อใดก็ตามที่ได้รับ :validate
การมีอยู่จะถูกตรวจสอบโดยอัตโนมัติ ให้ allow_blank: true
เพื่อแทนที่
ในที่สุด เมื่อ :validate
เป็นสัญลักษณ์ วิธีการที่กำหนดให้เป็นสัญลักษณ์จะถูกเรียก จากนั้นคุณสามารถเพิ่มการตรวจสอบได้เช่นเดียวกับที่คุณทำใน Active Record ( errors.add
)
:attachment
- เมื่อได้รับ ให้คาดว่าจะส่งไฟล์และแนบไปกับอีเมล อย่าลืมตั้งค่าแบบฟอร์มของคุณเป็นหลายประเภท นอกจากนี้ยังยอมรับไฟล์หลายไฟล์ผ่านแอตทริบิวต์ไฟล์แนบเดียว และจะแนบไฟล์ทีละไฟล์ไปกับอีเมล
:captcha
- เมื่อเป็นจริง ตรวจสอบแอตทริบิวต์จะต้องเว้นว่างไว้ นี่เป็นวิธีง่ายๆ ในการหลีกเลี่ยงสแปม และอินพุตควรซ่อนไว้ด้วย CSS
ตัวอย่าง:
class ContactForm < MailForm :: Base
attributes :name , validate : true
attributes :email , validate : / A [^@ s ]+@[^@ s ]+ z /i
attributes :type , validate : [ "General" , "Interface bug" ]
attributes :message
attributes :screenshot , attachment : true , validate : :interface_bug?
attributes :nickname , captcha : true
def interface_bug?
if type == 'Interface bug' && screenshot . nil?
self . errors . add ( :screenshot , "can't be blank on interface bugs" )
end
end
end
c = ContactForm . new ( nickname : 'not_blank' , email : '[email protected]' , name : 'José' )
c . valid? #=> true
c . spam? #=> true (raises an error in development, to remember you to hide it)
c . deliver #=> false (just delivers if is not a spam and is valid, raises an error in development)
c = ContactForm . new ( email : 'invalid' )
c . valid? #=> false
c . errors . inspect #=> { name: :blank, email: :invalid }
c . errors . full_messages #=> [ "Name can't be blank", "Email is invalid" ]
c = ContactForm . new ( name : 'José' , email : '[email protected]' )
c . deliver
MailForm ยังทำให้ง่ายต่อการผนวกข้อมูลคำขอจากไคลเอนต์ไปยังจดหมายที่ส่ง คุณเพียงแค่ต้องทำ:
class ContactForm < MailForm :: Base
append :remote_ip , :user_agent , :session
# ...
end
และในตัวควบคุมของคุณ:
@contact_form = ContactForm . new ( params [ :contact_form ] )
@contact_form . request = request
IP ระยะไกล ตัวแทนผู้ใช้ และเซสชันจะถูกส่งไปทางอีเมลในเซสชันข้อมูลคำขอ คุณสามารถกำหนดให้ผนวกวิธีการใดๆ ที่วัตถุคำขอตอบสนองได้
I18n ใน MailForm ทำงานเหมือนกับใน ActiveRecord ดังนั้นโมเดล คุณลักษณะ และข้อความทั้งหมดจึงสามารถใช้กับภาษาท้องถิ่นได้ ด้านล่างนี้เป็นไฟล์ตัวอย่างไฟล์ I18n:
mail_form :
models :
contact_form : "Your site contact form"
attributes :
contact_form :
email : "E-mail"
telephone : "Telephone number"
message : "Sent message"
request :
title : "Technical information about the user"
remote_ip : "IP Address"
user_agent : "Browser"
หากต้องการปรับแต่งเทมเพลตอีเมลที่ใช้ ให้สร้างไฟล์ชื่อ contact.erb
ใน app/views/mail_form
ดูที่ lib/mail_form/views/mail_form/contact.erb
ใน repo นี้เพื่อดูว่าเทมเพลตเริ่มต้นทำงานอย่างไร
เราตั้งใจที่จะรักษาการสนับสนุนสำหรับ Ruby / Rails เวอร์ชันทั้งหมดที่ยังไม่หมดอายุการใช้งาน
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเวอร์ชันเฉพาะ โปรดตรวจสอบนโยบายการบำรุงรักษา Ruby และ Rails และเมทริกซ์การทดสอบของเรา
หากคุณพบข้อบกพร่องใดๆ โปรดใช้ตัวติดตามปัญหา GitHub
ใบอนุญาตเอ็มไอที ลิขสิทธิ์ 2020-2024 ราฟาเอล ฟรานซา, คาร์ลอส อันโตนิโอ ดา ซิลวา ลิขสิทธิ์ 2009-2019 Plataformatec