我想创建一个联系表单,我在某个论坛中找到了mail_form gem。我试图使用它,但没有让它工作。以下是我的代码段: Ruby 1.8.7 Rails 2.3.15 mail_form 1.0.0
contact_form_controller.rb
require 'mail_form'
class ContactFormController < ApplicationController
def new
@contact_form = ContactForm.new
respond_to do |format|
format.html
format.xml
end
end
def create
begin
@contact_form = ContactForm.new(params[:contact_form])
@contact_form.request = request
if @contact_form.deliver
flash.now[:notice] = 'Thank you for your message!'
else
render :new
end
end
end
end
contact_form.rb 要求'mail_form'
class ContactForm < MailForm
subject "My Contact Form"
recipients "abc@gmail.com"
sender{|c| %{"#{c.name}" <#{c.email}>}}
end
new.html.erb
<% form_for @contact_form do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.lable :message %>
<%= f.text_area :message %><br/>
<%= f.submit 'Submit' %>
<% end %>
任何建议都会对我有所帮助。提前谢谢。
答案 0 :(得分:0)
根据文档,您的ContactForm应该是这样的:
class ContactForm < MailForm
subject "My Contact Form"
recipients "your.email@your.domain.com"
sender{|c| %{"#{c.name}" <#{c.email}>} }
attribute :name, :validate => true
attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
attribute :message
end