下面是一些我們正在使用的代碼:
控制器
#Form submitted
def enquire
@message = ContactForm.new(enquiry_params)
flash.clear
if @message.valid?
@message.deliver
flash[:notice] = "Message Sent Thank You!"
else
flash[:error] = "Sorry, You Got Errors!"
end
respond_to do |format|
format.html { redirect_to contact_index_path }
format.js
end
end
private
def enquire_params
params.require(:contact_form).permit(:name, :email, :message)
end
梅勒
我們已經設定,以便在窗體採用了一種名爲「ContactForm`郵件:
#app/mailers/contact_form.rb
class ContactForm < MailForm::Base
attribute :name, validate: true
attribute :email, validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
#Simple SPAM Protection (needs to remain blank)
attributes :nickname, captcha: true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
subject: "Website Enquiry",
to: AppConfig["company"]["enquiry"],
from: %("#{name}" <#{email}>)
}
end
end
查看
這然後使用:
#app/views/enquiry_mailer/new_message.text
Email: <%= @message.email %>
Subject: <%= @message.subject %>
Message: <%= @message.body %>
確定你有這一切嗎?
我試圖清理代碼,使其可讀,但我迷路了,你實際上正在嘗試做 - 'end' /'else'沒有任何意義,而你錯過了'end' 。 – sevenseacat
在'end'之後放置'else'沒有意義 – RSB