0

我想创建一个联系表单,我在某个论坛中找到了 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

新的.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 %>

任何建议都会对我有很大帮助。提前致谢。

4

1 回答 1

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

来自:http ://rubydoc.info/gems/mail_form/1.0.0/frames

于 2013-11-15T11:35:54.083 回答